Skip to content

Commit

Permalink
Add query to fetch license keys for enterprise accounts (#1402)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Jan 31, 2025
1 parent 33a4a62 commit 41d6aff
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 0 deletions.
14 changes: 14 additions & 0 deletions apps/core/lib/core/services/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,20 @@ defmodule Core.Services.Accounts do
do: allow(service_account, user, :impersonate)


@doc """
Grabs the license key for a users account
"""
@spec license_key(User.t) :: {:ok, binary} | {:error, term}
def license_key(%User{} = user) do
with {:ent, true} <- {:ent, Payments.enterprise?(user)},
{:ok, _} <- Payments.allow_billing(user) do
license_key()
else
{:ent, _} -> {:error, "only enterprise accounts can download license keys"}
err -> err
end
end

def license_key() do
exp = Timex.now() |> Timex.shift(days: 365) |> Timex.to_unix()
with {:ok, claims} <- Jwt.generate_claims(%{"enterprise" => true, "exp" => exp}),
Expand Down
5 changes: 5 additions & 0 deletions apps/core/lib/core/services/payments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ defmodule Core.Services.Payments do
def allow(%Subscription{} = subscription, %User{} = user),
do: allow(subscription, user, :access)

def allow_billing(%User{} = user) do
%{account: account} = Repo.preload(user, [:account])
allow(account, user, :pay)
end

@doc """
List all invoices against a subscription
"""
Expand Down
27 changes: 27 additions & 0 deletions apps/core/test/services/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -794,4 +794,31 @@ defmodule Core.Services.AccountsTest do
assert updated.usage_updated
end
end

describe "#license_key/1" do
test "enterprise billing users can download license keys" do
account = insert(:account, billing_customer_id: "cus_id", user_count: 2, cluster_count: 0)
user = insert(:user, roles: %{admin: true}, account: account)
enterprise_plan(account)

{:ok, key} = Accounts.license_key(user)

assert is_binary(key)
end

test "non billing users cannot downlod license keys" do
account = insert(:account, billing_customer_id: "cus_id", user_count: 2, cluster_count: 0)
user = insert(:user, account: account)
enterprise_plan(account)

{:error, _} = Accounts.license_key(user)
end

test "non enterprise accounts cannot downlod license keys" do
account = insert(:account, billing_customer_id: "cus_id", user_count: 2, cluster_count: 0)
user = insert(:user, roles: %{admin: true}, account: account)

{:error, _} = Accounts.license_key(user)
end
end
end
3 changes: 3 additions & 0 deletions apps/graphql/lib/graphql/resolvers/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ defmodule GraphQl.Resolvers.Account do
def resolve_invite(%{id: secure_id}, _),
do: {:ok, Accounts.get_invite(secure_id)}

def license_key(_, %{context: %{current_user: user}}),
do: Accounts.license_key(user)

def create_service_account(%{attributes: attrs}, %{context: %{current_user: user}}),
do: Accounts.create_service_account(attrs, user)

Expand Down
6 changes: 6 additions & 0 deletions apps/graphql/lib/graphql/schema/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ defmodule GraphQl.Schema.Account do

resolve &Account.list_oauth_integrations/2
end

field :license_key, :string do
middleware Authenticated

resolve &Account.license_key/2
end
end

object :account_mutations do
Expand Down
16 changes: 16 additions & 0 deletions apps/graphql/test/queries/account_queries_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,20 @@ defmodule GraphQl.AccountQueriesTest do
|> ids_equal(invites)
end
end

describe "licenseKey" do
test "enterprise billing users can download license keys" do
account = insert(:account, billing_customer_id: "cus_id", user_count: 2, cluster_count: 0)
user = insert(:user, roles: %{admin: true}, account: account)
enterprise_plan(account)

{:ok, %{data: %{"licenseKey" => k}}} = run_query("""
query {
licenseKey
}
""", %{}, %{current_user: user})

assert is_binary(k)
end
end
end
2 changes: 2 additions & 0 deletions schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ type RootQueryType {

oauthIntegrations: [OauthIntegration]

licenseKey: String

incidents(
after: String, first: Int, before: String, last: Int, repositoryId: ID, supports: Boolean, q: String, sort: IncidentSort, order: Order, filters: [IncidentFilter]
): IncidentConnection
Expand Down
1 change: 1 addition & 0 deletions www/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3806,6 +3806,7 @@ export type RootQueryType = {
invoices?: Maybe<InvoiceConnection>;
keyBackup?: Maybe<KeyBackup>;
keyBackups?: Maybe<KeyBackupConnection>;
licenseKey?: Maybe<Scalars['String']['output']>;
loginMethod?: Maybe<LoginMethodResponse>;
loginMetrics?: Maybe<Array<Maybe<GeoMetric>>>;
me?: Maybe<User>;
Expand Down

0 comments on commit 41d6aff

Please sign in to comment.