-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(monosize-storage-azure): add support for federated identity with…
… azure pipelines (#79)
- Loading branch information
1 parent
ce8d61e
commit e968bcd
Showing
10 changed files
with
466 additions
and
91 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/monosize-storage-azure-a9e6fc6b-938e-4645-95c4-32659838f654.json
This file contains 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,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "feat: add support for workload identity authentication with azure pipelines.", | ||
"packageName": "monosize-storage-azure", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
This file contains 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 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 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,67 @@ | ||
import { AzureNamedKeyCredential, TableClient } from '@azure/data-tables'; | ||
import { AzurePipelinesCredential } from '@azure/identity'; | ||
import type { AzureStorageConfig } from './types.mjs'; | ||
|
||
export function createTableClient(options: Required<Pick<AzureStorageConfig, 'authType' | 'tableName'>>): TableClient { | ||
const { authType, tableName } = options; | ||
|
||
const AZURE_STORAGE_TABLE_NAME = tableName; | ||
|
||
if (authType === 'AzureNamedKeyCredential') { | ||
const requiredEnvVars = ['BUNDLESIZE_ACCOUNT_NAME', 'BUNDLESIZE_ACCOUNT_KEY']; | ||
validateRequiredEnvVariables({ | ||
requiredEnvVars, | ||
authType, | ||
}); | ||
|
||
const AZURE_STORAGE_ACCOUNT = process.env['BUNDLESIZE_ACCOUNT_NAME'] as string; | ||
const AZURE_ACCOUNT_KEY = process.env['BUNDLESIZE_ACCOUNT_KEY'] as string; | ||
|
||
return new TableClient( | ||
`https://${AZURE_STORAGE_ACCOUNT}.table.core.windows.net`, | ||
AZURE_STORAGE_TABLE_NAME, | ||
new AzureNamedKeyCredential(AZURE_STORAGE_ACCOUNT, AZURE_ACCOUNT_KEY), | ||
); | ||
} | ||
|
||
if (authType === 'AzurePipelinesCredential') { | ||
const requiredEnvVars = [ | ||
'BUNDLESIZE_ACCOUNT_NAME', | ||
'AZURE_TENANT_ID', | ||
'AZURE_CLIENT_ID', | ||
'AZURE_SERVICE_CONNECTION_ID', | ||
'SYSTEM_ACCESSTOKEN', | ||
]; | ||
validateRequiredEnvVariables({ | ||
requiredEnvVars, | ||
authType, | ||
}); | ||
|
||
const AZURE_STORAGE_ACCOUNT = process.env['BUNDLESIZE_ACCOUNT_NAME'] as string; | ||
const TENANT_ID = process.env['AZURE_TENANT_ID'] as string; | ||
const CLIENT_ID = process.env['AZURE_CLIENT_ID'] as string; | ||
const SERVICE_CONNECTION_ID = process.env['AZURE_SERVICE_CONNECTION_ID'] as string; | ||
const SYSTEM_ACCESSTOKEN = process.env['SYSTEM_ACCESSTOKEN'] as string; | ||
|
||
return new TableClient( | ||
`https://${AZURE_STORAGE_ACCOUNT}.table.core.windows.net`, | ||
AZURE_STORAGE_TABLE_NAME, | ||
new AzurePipelinesCredential(TENANT_ID, CLIENT_ID, SERVICE_CONNECTION_ID, SYSTEM_ACCESSTOKEN), | ||
); | ||
} | ||
|
||
throw new Error(`monosize-storage-azure: "authType: ${authType}" is not supported.`); | ||
} | ||
|
||
function validateRequiredEnvVariables(options: { requiredEnvVars: string[]; authType: string }): void { | ||
const { requiredEnvVars, authType } = options; | ||
const missingEnvVars = requiredEnvVars.filter(envParamName => typeof process.env[envParamName] !== 'string'); | ||
|
||
if (missingEnvVars.length > 0) { | ||
throw new Error( | ||
`monosize-storage-azure: Missing required environment variable(s) for authType ${authType}: ${missingEnvVars.join( | ||
', ', | ||
)} not in your process.env.`, | ||
); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/monosize-storage-azure/src/createTableClient.test.mts
This file contains 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,85 @@ | ||
import { beforeEach, describe, expect, it, vitest } from 'vitest'; | ||
import { AzureNamedKeyCredential, TableClient } from '@azure/data-tables'; | ||
import { AzurePipelinesCredential } from '@azure/identity'; | ||
import { createTableClient } from './createTableClient.mjs'; | ||
import type { AzureAuthenticationType } from './types.mjs'; | ||
|
||
vitest.mock('@azure/data-tables', () => { | ||
return { | ||
AzureNamedKeyCredential: vitest.fn(), | ||
TableClient: vitest.fn().mockImplementation(() => { | ||
return { | ||
createTable: vitest.fn(), | ||
deleteTable: vitest.fn(), | ||
}; | ||
}), | ||
}; | ||
}); | ||
|
||
vitest.mock('@azure/identity', () => { | ||
return { | ||
AzurePipelinesCredential: vitest.fn(), | ||
}; | ||
}); | ||
|
||
describe('createTableClient', () => { | ||
beforeEach(() => { | ||
vitest.resetAllMocks(); | ||
vitest.unstubAllEnvs(); | ||
}); | ||
|
||
it('should create TableClient with AzureNamedKeyCredential', () => { | ||
vitest.stubEnv('BUNDLESIZE_ACCOUNT_NAME', 'test-account-name'); | ||
vitest.stubEnv('BUNDLESIZE_ACCOUNT_KEY', 'test-account-key'); | ||
|
||
const authType = 'AzureNamedKeyCredential'; | ||
const tableName = 'test-table'; | ||
createTableClient({ authType, tableName }); | ||
|
||
expect(AzureNamedKeyCredential).toHaveBeenCalledWith( | ||
process.env['BUNDLESIZE_ACCOUNT_NAME'] as string, | ||
process.env['BUNDLESIZE_ACCOUNT_KEY'] as string, | ||
); | ||
|
||
expect(TableClient).toHaveBeenCalledWith( | ||
'https://test-account-name.table.core.windows.net', | ||
tableName, | ||
expect.any(AzureNamedKeyCredential), | ||
); | ||
}); | ||
|
||
it('should create TableClient with AzurePipelinesCredential', () => { | ||
vitest.stubEnv('BUNDLESIZE_ACCOUNT_NAME', 'test-account-name'); | ||
vitest.stubEnv('AZURE_TENANT_ID', 'test-tenant-id'); | ||
vitest.stubEnv('AZURE_CLIENT_ID', 'test-client-id'); | ||
vitest.stubEnv('AZURE_SERVICE_CONNECTION_ID', 'test-service-connection-id'); | ||
vitest.stubEnv('SYSTEM_ACCESSTOKEN', 'test-system-access-token'); | ||
vitest.stubEnv('SYSTEM_OIDCREQUESTURI', 'test-system-oidc-request-uri'); | ||
|
||
const authType = 'AzurePipelinesCredential'; | ||
const tableName = 'test-table'; | ||
createTableClient({ authType, tableName }); | ||
|
||
expect(AzurePipelinesCredential).toHaveBeenCalledWith( | ||
process.env['AZURE_TENANT_ID'] as string, | ||
process.env['AZURE_CLIENT_ID'] as string, | ||
process.env['AZURE_SERVICE_CONNECTION_ID'] as string, | ||
process.env['SYSTEM_ACCESSTOKEN'] as string, | ||
); | ||
|
||
expect(TableClient).toHaveBeenCalledWith( | ||
'https://test-account-name.table.core.windows.net', | ||
tableName, | ||
expect.any(AzurePipelinesCredential), | ||
); | ||
}); | ||
|
||
it('should throw an error for unsupported authType', () => { | ||
const authType = 'AzureNamedKeyCredentail' as AzureAuthenticationType; | ||
const tableName = 'test-table'; | ||
|
||
expect(() => createTableClient({ authType, tableName })).toThrow( | ||
`monosize-storage-azure: "authType: ${authType}" is not supported.`, | ||
); | ||
}); | ||
}); |
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,16 @@ | ||
/** | ||
* @see https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-azure-hosted-applications | ||
*/ | ||
export type AzureAuthenticationType = 'AzureNamedKeyCredential' | 'AzurePipelinesCredential'; | ||
|
||
export type AzureStorageConfig = { | ||
endpoint: string; | ||
/** | ||
* @default 'AzureNamedKeyCredential' auth type | ||
*/ | ||
authType?: AzureAuthenticationType; | ||
/** | ||
* @default 'latest' table name | ||
*/ | ||
tableName?: string; | ||
}; |
This file contains 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 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.