Repository navigation
fix(aws-marketplace): check the container license with CheckoutLicense so AWS's standard buyer policy works #9981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e5da163
fix(aws-marketplace): check the container license with CheckoutLicens…
waleedlatif1 a0b6763
fix(aws-marketplace): count only an unbroken run of no-entitlement an…
waleedlatif1 e518dd0
fix(aws-marketplace): check the container license as an uncounted tie…
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
apps/sim/lib/billing/aws-marketplace/container-license.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockSend } = vi.hoisted(() => ({ mockSend: vi.fn() })) | ||
|
|
||
| vi.mock('@aws-sdk/client-license-manager', () => ({ | ||
| LicenseManagerClient: class { | ||
| send = mockSend | ||
| }, | ||
| CheckoutLicenseCommand: class { | ||
| constructor(public input: { ProductSKU: string }) {} | ||
| }, | ||
| })) | ||
| vi.mock('@/lib/core/network/proxy-agent.server', () => ({ awsRequestHandler: () => undefined })) | ||
| vi.mock('@sim/utils/random', () => ({ randomInt: (min: number) => min })) | ||
|
|
||
| import { applyAwsMarketplaceContainerLicense } from '@/lib/billing/aws-marketplace/container-license' | ||
|
|
||
| const MINUTE = 60 * 1000 | ||
|
|
||
| type Answer = | ||
| | 'entitled' | ||
| | 'NoEntitlementsAllowedException' | ||
| | 'ResourceNotFoundException' | ||
| | 'AccessDeniedException' | ||
| | 'ServerInternalException' | ||
|
|
||
| /** License Manager's checkout answers in order; the last one repeats once the list runs out. */ | ||
| let answers: Answer[] = [] | ||
| const checkoutRequests: unknown[] = [] | ||
|
|
||
| function awsError(name: string): Error { | ||
| return Object.assign(new Error(name), { name }) | ||
| } | ||
|
|
||
| function enterpriseEnabled(): boolean { | ||
| return process.env.ENTERPRISE_ENABLED === 'true' | ||
| } | ||
|
|
||
| describe('applyAwsMarketplaceContainerLicense', () => { | ||
| let kill: ReturnType<typeof vi.spyOn> | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers({ toFake: ['setTimeout'] }) | ||
| answers = [] | ||
| checkoutRequests.length = 0 | ||
| vi.stubEnv('AWS_MARKETPLACE_LICENSE_PRODUCT_SKU', 'product-placeholder') | ||
| kill = vi.spyOn(process, 'kill').mockImplementation(() => true) | ||
| mockSend.mockReset() | ||
| mockSend.mockImplementation(async (command: { input: unknown }) => { | ||
| checkoutRequests.push(command.input) | ||
| const answer = answers.length > 1 ? answers.shift() : answers[0] | ||
| if (answer !== 'entitled') throw awsError(answer ?? 'ServerInternalException') | ||
| return { EntitlementsAllowed: [{ Name: 'enterprise_license', Unit: 'None' }] } | ||
| }) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllTimers() | ||
| vi.useRealTimers() | ||
| kill.mockRestore() | ||
| Reflect.deleteProperty(process.env, 'ENTERPRISE_ENABLED') | ||
| Reflect.deleteProperty(process.env, 'NEXT_PUBLIC_ENTERPRISE_ENABLED') | ||
| }) | ||
|
|
||
| it('does nothing outside a Marketplace install', async () => { | ||
| vi.stubEnv('AWS_MARKETPLACE_LICENSE_PRODUCT_SKU', undefined) | ||
| await applyAwsMarketplaceContainerLicense() | ||
| expect(mockSend).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('turns Enterprise on with an uncounted checkout of the entitlement', async () => { | ||
| answers = ['entitled'] | ||
| await applyAwsMarketplaceContainerLicense() | ||
| expect(enterpriseEnabled()).toBe(true) | ||
| expect(checkoutRequests).toEqual([ | ||
| expect.objectContaining({ | ||
| ProductSKU: 'product-placeholder', | ||
| CheckoutType: 'PROVISIONAL', | ||
| KeyFingerprint: 'aws:294406891311:AWS/Marketplace:issuer-fingerprint', | ||
| Entitlements: [{ Name: 'enterprise_license', Unit: 'None' }], | ||
| }), | ||
| ]) | ||
| }) | ||
|
|
||
| it('runs without Enterprise when no license grants the entitlement', async () => { | ||
| answers = ['NoEntitlementsAllowedException'] | ||
| await applyAwsMarketplaceContainerLicense() | ||
| expect(enterpriseEnabled()).toBe(false) | ||
| }) | ||
|
|
||
| it('runs without Enterprise when its IAM role cannot check out', async () => { | ||
| answers = ['AccessDeniedException'] | ||
| await applyAwsMarketplaceContainerLicense() | ||
| expect(enterpriseEnabled()).toBe(false) | ||
| }) | ||
|
|
||
| it.each<Answer>(['NoEntitlementsAllowedException', 'ResourceNotFoundException'])( | ||
| 'restarts without Enterprise once the license ends (%s)', | ||
| async (answer) => { | ||
| answers = ['entitled', answer] | ||
| await applyAwsMarketplaceContainerLicense() | ||
| await vi.advanceTimersByTimeAsync(16 * MINUTE) | ||
| expect(kill).toHaveBeenCalledWith(process.pid, 'SIGTERM') | ||
| } | ||
| ) | ||
|
|
||
| it('restarts into Enterprise once the license becomes active', async () => { | ||
| answers = ['NoEntitlementsAllowedException', 'entitled'] | ||
| await applyAwsMarketplaceContainerLicense() | ||
| expect(enterpriseEnabled()).toBe(false) | ||
| await vi.advanceTimersByTimeAsync(16 * MINUTE) | ||
| expect(kill).toHaveBeenCalledWith(process.pid, 'SIGTERM') | ||
| }) | ||
|
|
||
| it.each<Answer>(['ServerInternalException', 'AccessDeniedException'])( | ||
| 'never restarts a running Enterprise deployment over a failed check (%s)', | ||
| async (answer) => { | ||
| answers = ['entitled', answer] | ||
| await applyAwsMarketplaceContainerLicense() | ||
| await vi.advanceTimersByTimeAsync(6 * 60 * MINUTE) | ||
| expect(kill).not.toHaveBeenCalled() | ||
| } | ||
| ) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.