@backstage/integration 1.4.5-next.0 → 1.5.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -182,7 +182,46 @@ type AzureIntegrationConfig = {
182
182
  * If no token is specified, anonymous access is used.
183
183
  */
184
184
  token?: string;
185
+ /**
186
+ * The credential to use for requests.
187
+ *
188
+ * If no credential is specified anonymous access is used.
189
+ */
190
+ credential?: AzureCredential;
191
+ };
192
+ /**
193
+ * Authenticate using a client secret that was generated for an App Registration.
194
+ * @public
195
+ */
196
+ type ClientSecret = {
197
+ /**
198
+ * The Azure Active Directory tenant
199
+ */
200
+ tenantId: string;
201
+ /**
202
+ * The client id
203
+ */
204
+ clientId: string;
205
+ /**
206
+ * The client secret
207
+ */
208
+ clientSecret: string;
185
209
  };
210
+ /**
211
+ * Authenticate using a managed identity available at the deployment environment.
212
+ * @public
213
+ */
214
+ type ManagedIdentity = {
215
+ /**
216
+ * The clientId
217
+ */
218
+ clientId: string;
219
+ };
220
+ /**
221
+ * Credential used to authenticate to Azure Active Directory.
222
+ * @public
223
+ */
224
+ type AzureCredential = ClientSecret | ManagedIdentity;
186
225
  /**
187
226
  * Reads a single Azure integration config.
188
227
  *
@@ -252,11 +291,12 @@ declare function getAzureCommitsUrl(url: string): string;
252
291
  * Gets the request options necessary to make requests to a given provider.
253
292
  *
254
293
  * @param config - The relevant provider config
294
+ * @param additionalHeaders - Additional headers for the request
255
295
  * @public
256
296
  */
257
- declare function getAzureRequestOptions(config: AzureIntegrationConfig, additionalHeaders?: Record<string, string>): {
297
+ declare function getAzureRequestOptions(config: AzureIntegrationConfig, additionalHeaders?: Record<string, string>): Promise<{
258
298
  headers: Record<string, string>;
259
- };
299
+ }>;
260
300
 
261
301
  /**
262
302
  * The configuration parameters for a single Bitbucket API provider.
@@ -1317,6 +1357,38 @@ declare function getGitLabRequestOptions(config: GitLabIntegrationConfig): {
1317
1357
  headers: Record<string, string>;
1318
1358
  };
1319
1359
 
1360
+ /**
1361
+ * @public
1362
+ */
1363
+ type GitlabCredentials = {
1364
+ headers?: {
1365
+ [name: string]: string;
1366
+ };
1367
+ token?: string;
1368
+ };
1369
+ /**
1370
+ * @public
1371
+ */
1372
+ interface GitlabCredentialsProvider {
1373
+ getCredentials(opts: {
1374
+ url: string;
1375
+ }): Promise<GitlabCredentials>;
1376
+ }
1377
+
1378
+ /**
1379
+ * Handles the creation and caching of credentials for GitLab integrations.
1380
+ *
1381
+ * @public
1382
+ */
1383
+ declare class DefaultGitlabCredentialsProvider implements GitlabCredentialsProvider {
1384
+ private readonly providers;
1385
+ static fromIntegrations(integrations: ScmIntegrationRegistry): DefaultGitlabCredentialsProvider;
1386
+ private constructor();
1387
+ getCredentials(opts: {
1388
+ url: string;
1389
+ }): Promise<GitlabCredentials>;
1390
+ }
1391
+
1320
1392
  /**
1321
1393
  * The configuration parameters for a single Google Cloud Storage provider.
1322
1394
  *
@@ -1403,4 +1475,4 @@ declare class ScmIntegrations implements ScmIntegrationRegistry {
1403
1475
  resolveEditUrl(url: string): string;
1404
1476
  }
1405
1477
 
1406
- export { AwsS3Integration, AwsS3IntegrationConfig, AzureIntegration, AzureIntegrationConfig, BitbucketCloudIntegration, BitbucketCloudIntegrationConfig, BitbucketIntegration, BitbucketIntegrationConfig, BitbucketServerIntegration, BitbucketServerIntegrationConfig, DefaultGithubCredentialsProvider, GerritIntegration, GerritIntegrationConfig, GitHubIntegration, GitHubIntegrationConfig, GitLabIntegration, GitLabIntegrationConfig, GiteaIntegration, GiteaIntegrationConfig, GithubAppConfig, GithubAppCredentialsMux, GithubCredentialType, GithubCredentials, GithubCredentialsProvider, GithubIntegration, GithubIntegrationConfig, GoogleGcsIntegrationConfig, IntegrationsByType, ScmIntegration, ScmIntegrationRegistry, ScmIntegrations, ScmIntegrationsFactory, ScmIntegrationsGroup, SingleInstanceGithubCredentialsProvider, defaultScmResolveUrl, getAzureCommitsUrl, getAzureDownloadUrl, getAzureFileFetchUrl, getAzureRequestOptions, getBitbucketCloudDefaultBranch, getBitbucketCloudDownloadUrl, getBitbucketCloudFileFetchUrl, getBitbucketCloudRequestOptions, getBitbucketDefaultBranch, getBitbucketDownloadUrl, getBitbucketFileFetchUrl, getBitbucketRequestOptions, getBitbucketServerDefaultBranch, getBitbucketServerDownloadUrl, getBitbucketServerFileFetchUrl, getBitbucketServerRequestOptions, getGerritBranchApiUrl, getGerritCloneRepoUrl, getGerritFileContentsApiUrl, getGerritProjectsApiUrl, getGerritRequestOptions, getGitHubFileFetchUrl, getGitHubRequestOptions, getGitLabFileFetchUrl, getGitLabIntegrationRelativePath, getGitLabRequestOptions, getGiteaFileContentsUrl, getGiteaRequestOptions, getGithubFileFetchUrl, parseGerritGitilesUrl, parseGerritJsonResponse, readAwsS3IntegrationConfig, readAwsS3IntegrationConfigs, readAzureIntegrationConfig, readAzureIntegrationConfigs, readBitbucketCloudIntegrationConfig, readBitbucketCloudIntegrationConfigs, readBitbucketIntegrationConfig, readBitbucketIntegrationConfigs, readBitbucketServerIntegrationConfig, readBitbucketServerIntegrationConfigs, readGerritIntegrationConfig, readGerritIntegrationConfigs, readGitHubIntegrationConfig, readGitHubIntegrationConfigs, readGitLabIntegrationConfig, readGitLabIntegrationConfigs, readGiteaConfig, readGithubIntegrationConfig, readGithubIntegrationConfigs, readGoogleGcsIntegrationConfig, replaceGitHubUrlType, replaceGitLabUrlType, replaceGithubUrlType };
1478
+ export { AwsS3Integration, AwsS3IntegrationConfig, AzureCredential, AzureIntegration, AzureIntegrationConfig, BitbucketCloudIntegration, BitbucketCloudIntegrationConfig, BitbucketIntegration, BitbucketIntegrationConfig, BitbucketServerIntegration, BitbucketServerIntegrationConfig, ClientSecret, DefaultGithubCredentialsProvider, DefaultGitlabCredentialsProvider, GerritIntegration, GerritIntegrationConfig, GitHubIntegration, GitHubIntegrationConfig, GitLabIntegration, GitLabIntegrationConfig, GiteaIntegration, GiteaIntegrationConfig, GithubAppConfig, GithubAppCredentialsMux, GithubCredentialType, GithubCredentials, GithubCredentialsProvider, GithubIntegration, GithubIntegrationConfig, GitlabCredentials, GitlabCredentialsProvider, GoogleGcsIntegrationConfig, IntegrationsByType, ManagedIdentity, ScmIntegration, ScmIntegrationRegistry, ScmIntegrations, ScmIntegrationsFactory, ScmIntegrationsGroup, SingleInstanceGithubCredentialsProvider, defaultScmResolveUrl, getAzureCommitsUrl, getAzureDownloadUrl, getAzureFileFetchUrl, getAzureRequestOptions, getBitbucketCloudDefaultBranch, getBitbucketCloudDownloadUrl, getBitbucketCloudFileFetchUrl, getBitbucketCloudRequestOptions, getBitbucketDefaultBranch, getBitbucketDownloadUrl, getBitbucketFileFetchUrl, getBitbucketRequestOptions, getBitbucketServerDefaultBranch, getBitbucketServerDownloadUrl, getBitbucketServerFileFetchUrl, getBitbucketServerRequestOptions, getGerritBranchApiUrl, getGerritCloneRepoUrl, getGerritFileContentsApiUrl, getGerritProjectsApiUrl, getGerritRequestOptions, getGitHubFileFetchUrl, getGitHubRequestOptions, getGitLabFileFetchUrl, getGitLabIntegrationRelativePath, getGitLabRequestOptions, getGiteaFileContentsUrl, getGiteaRequestOptions, getGithubFileFetchUrl, parseGerritGitilesUrl, parseGerritJsonResponse, readAwsS3IntegrationConfig, readAwsS3IntegrationConfigs, readAzureIntegrationConfig, readAzureIntegrationConfigs, readBitbucketCloudIntegrationConfig, readBitbucketCloudIntegrationConfigs, readBitbucketIntegrationConfig, readBitbucketIntegrationConfigs, readBitbucketServerIntegrationConfig, readBitbucketServerIntegrationConfigs, readGerritIntegrationConfig, readGerritIntegrationConfigs, readGitHubIntegrationConfig, readGitHubIntegrationConfigs, readGitLabIntegrationConfig, readGitLabIntegrationConfigs, readGiteaConfig, readGithubIntegrationConfig, readGithubIntegrationConfigs, readGoogleGcsIntegrationConfig, replaceGitHubUrlType, replaceGitLabUrlType, replaceGithubUrlType };
package/dist/index.esm.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import parseGitUrl from 'git-url-parse';
2
2
  import { trimEnd, trimStart } from 'lodash';
3
+ import { ClientSecretCredential, ManagedIdentityCredential } from '@azure/identity';
3
4
  import fetch from 'cross-fetch';
4
5
  import { createAppAuth } from '@octokit/auth-app';
5
6
  import { Octokit } from '@octokit/rest';
@@ -334,16 +335,43 @@ _ref = new WeakMap();
334
335
  _baseUrl = new WeakMap();
335
336
 
336
337
  const AZURE_HOST = "dev.azure.com";
338
+ const isServicePrincipal = (credential) => {
339
+ const clientSecretCredential = credential;
340
+ return Object.keys(credential).length === 3 && clientSecretCredential.clientId !== void 0 && clientSecretCredential.clientSecret !== void 0 && clientSecretCredential.tenantId !== void 0;
341
+ };
342
+ const isManagedIdentity = (credential) => {
343
+ return Object.keys(credential).length === 1 && credential.clientId !== void 0;
344
+ };
337
345
  function readAzureIntegrationConfig(config) {
338
346
  var _a;
339
347
  const host = (_a = config.getOptionalString("host")) != null ? _a : AZURE_HOST;
340
348
  const token = config.getOptionalString("token");
349
+ const credential = config.getOptional("credential") ? {
350
+ tenantId: config.getOptionalString("credential.tenantId"),
351
+ clientId: config.getOptionalString("credential.clientId"),
352
+ clientSecret: config.getOptionalString("credential.clientSecret")
353
+ } : void 0;
341
354
  if (!isValidHost(host)) {
342
355
  throw new Error(
343
356
  `Invalid Azure integration config, '${host}' is not a valid host`
344
357
  );
345
358
  }
346
- return { host, token };
359
+ if (credential && !isServicePrincipal(credential) && !isManagedIdentity(credential)) {
360
+ throw new Error(
361
+ `Invalid Azure integration config, credential is not valid`
362
+ );
363
+ }
364
+ if (credential && host !== AZURE_HOST) {
365
+ throw new Error(
366
+ `Invalid Azure integration config, credential can only be used with ${AZURE_HOST}`
367
+ );
368
+ }
369
+ if (credential && token) {
370
+ throw new Error(
371
+ `Invalid Azure integration config, specify either a token or a credential but not both`
372
+ );
373
+ }
374
+ return { host, token, credential };
347
375
  }
348
376
  function readAzureIntegrationConfigs(configs) {
349
377
  const result = configs.map(readAzureIntegrationConfig);
@@ -419,9 +447,27 @@ function getAzureDownloadUrl(url) {
419
447
  function getAzureCommitsUrl(url) {
420
448
  return AzureUrl.fromRepoUrl(url).toCommitsUrl();
421
449
  }
422
- function getAzureRequestOptions(config, additionalHeaders) {
450
+ async function getAzureRequestOptions(config, additionalHeaders) {
451
+ const azureDevOpsScope = "499b84ac-1321-427f-aa17-267ca6975798/.default";
423
452
  const headers = additionalHeaders ? { ...additionalHeaders } : {};
424
- if (config.token) {
453
+ const { token, credential } = config;
454
+ if (credential) {
455
+ if (isServicePrincipal(credential)) {
456
+ const servicePrincipal = new ClientSecretCredential(
457
+ credential.tenantId,
458
+ credential.clientId,
459
+ credential.clientSecret
460
+ );
461
+ const accessToken = await servicePrincipal.getToken(azureDevOpsScope);
462
+ headers.Authorization = `Bearer ${accessToken.token}`;
463
+ } else if (isManagedIdentity(credential)) {
464
+ const managedIdentity = new ManagedIdentityCredential(
465
+ credential.clientId
466
+ );
467
+ const accessToken = await managedIdentity.getToken(azureDevOpsScope);
468
+ headers.Authorization = `Bearer ${accessToken.token}`;
469
+ }
470
+ } else if (token) {
425
471
  const buffer = Buffer.from(`:${config.token}`, "utf8");
426
472
  headers.Authorization = `Basic ${buffer.toString("base64")}`;
427
473
  }
@@ -1304,8 +1350,8 @@ class GithubAppManager {
1304
1350
  const allInstallations = await this.getInstallations();
1305
1351
  const installation = allInstallations.find(
1306
1352
  (inst) => {
1307
- var _a, _b;
1308
- return ((_b = (_a = inst.account) == null ? void 0 : _a.login) == null ? void 0 : _b.toLocaleLowerCase("en-US")) === owner.toLocaleLowerCase("en-US");
1353
+ var _a;
1354
+ return inst.account && "login" in inst.account && ((_a = inst.account.login) == null ? void 0 : _a.toLocaleLowerCase("en-US")) === owner.toLocaleLowerCase("en-US");
1309
1355
  }
1310
1356
  );
1311
1357
  if (installation) {
@@ -1666,6 +1712,51 @@ function replaceGitLabUrlType(url, type) {
1666
1712
  return url.replace(/\/\-\/(blob|tree|edit)\//, `/-/${type}/`);
1667
1713
  }
1668
1714
 
1715
+ const _SingleInstanceGitlabCredentialsProvider = class {
1716
+ constructor(token) {
1717
+ this.token = token;
1718
+ }
1719
+ async getCredentials(_opts) {
1720
+ if (!this.token) {
1721
+ return {};
1722
+ }
1723
+ return {
1724
+ headers: {
1725
+ Authorization: `Bearer ${this.token}`
1726
+ },
1727
+ token: this.token
1728
+ };
1729
+ }
1730
+ };
1731
+ let SingleInstanceGitlabCredentialsProvider = _SingleInstanceGitlabCredentialsProvider;
1732
+ SingleInstanceGitlabCredentialsProvider.create = (config) => {
1733
+ return new _SingleInstanceGitlabCredentialsProvider(config.token);
1734
+ };
1735
+
1736
+ class DefaultGitlabCredentialsProvider {
1737
+ constructor(providers) {
1738
+ this.providers = providers;
1739
+ }
1740
+ static fromIntegrations(integrations) {
1741
+ const credentialsProviders = /* @__PURE__ */ new Map();
1742
+ integrations.gitlab.list().forEach((integration) => {
1743
+ const credentialsProvider = SingleInstanceGitlabCredentialsProvider.create(integration.config);
1744
+ credentialsProviders.set(integration.config.host, credentialsProvider);
1745
+ });
1746
+ return new DefaultGitlabCredentialsProvider(credentialsProviders);
1747
+ }
1748
+ async getCredentials(opts) {
1749
+ const parsed = new URL(opts.url);
1750
+ const provider = this.providers.get(parsed.host);
1751
+ if (!provider) {
1752
+ throw new Error(
1753
+ `There is no GitLab integration that matches ${opts.url}. Please add a configuration for an integration.`
1754
+ );
1755
+ }
1756
+ return provider.getCredentials(opts);
1757
+ }
1758
+ }
1759
+
1669
1760
  function readGoogleGcsIntegrationConfig(config) {
1670
1761
  if (!config) {
1671
1762
  return {};
@@ -1761,5 +1852,5 @@ class ScmIntegrations {
1761
1852
  }
1762
1853
  }
1763
1854
 
1764
- export { AwsS3Integration, AzureIntegration, BitbucketCloudIntegration, BitbucketIntegration, BitbucketServerIntegration, DefaultGithubCredentialsProvider, GerritIntegration, GitHubIntegration, GitLabIntegration, GiteaIntegration, GithubAppCredentialsMux, GithubIntegration, ScmIntegrations, SingleInstanceGithubCredentialsProvider, defaultScmResolveUrl, getAzureCommitsUrl, getAzureDownloadUrl, getAzureFileFetchUrl, getAzureRequestOptions, getBitbucketCloudDefaultBranch, getBitbucketCloudDownloadUrl, getBitbucketCloudFileFetchUrl, getBitbucketCloudRequestOptions, getBitbucketDefaultBranch, getBitbucketDownloadUrl, getBitbucketFileFetchUrl, getBitbucketRequestOptions, getBitbucketServerDefaultBranch, getBitbucketServerDownloadUrl, getBitbucketServerFileFetchUrl, getBitbucketServerRequestOptions, getGerritBranchApiUrl, getGerritCloneRepoUrl, getGerritFileContentsApiUrl, getGerritProjectsApiUrl, getGerritRequestOptions, getGitHubFileFetchUrl, getGitHubRequestOptions, getGitLabFileFetchUrl, getGitLabIntegrationRelativePath, getGitLabRequestOptions, getGiteaFileContentsUrl, getGiteaRequestOptions, getGithubFileFetchUrl, parseGerritGitilesUrl, parseGerritJsonResponse, readAwsS3IntegrationConfig, readAwsS3IntegrationConfigs, readAzureIntegrationConfig, readAzureIntegrationConfigs, readBitbucketCloudIntegrationConfig, readBitbucketCloudIntegrationConfigs, readBitbucketIntegrationConfig, readBitbucketIntegrationConfigs, readBitbucketServerIntegrationConfig, readBitbucketServerIntegrationConfigs, readGerritIntegrationConfig, readGerritIntegrationConfigs, readGitHubIntegrationConfig, readGitHubIntegrationConfigs, readGitLabIntegrationConfig, readGitLabIntegrationConfigs, readGiteaConfig, readGithubIntegrationConfig, readGithubIntegrationConfigs, readGoogleGcsIntegrationConfig, replaceGitHubUrlType, replaceGitLabUrlType, replaceGithubUrlType };
1855
+ export { AwsS3Integration, AzureIntegration, BitbucketCloudIntegration, BitbucketIntegration, BitbucketServerIntegration, DefaultGithubCredentialsProvider, DefaultGitlabCredentialsProvider, GerritIntegration, GitHubIntegration, GitLabIntegration, GiteaIntegration, GithubAppCredentialsMux, GithubIntegration, ScmIntegrations, SingleInstanceGithubCredentialsProvider, defaultScmResolveUrl, getAzureCommitsUrl, getAzureDownloadUrl, getAzureFileFetchUrl, getAzureRequestOptions, getBitbucketCloudDefaultBranch, getBitbucketCloudDownloadUrl, getBitbucketCloudFileFetchUrl, getBitbucketCloudRequestOptions, getBitbucketDefaultBranch, getBitbucketDownloadUrl, getBitbucketFileFetchUrl, getBitbucketRequestOptions, getBitbucketServerDefaultBranch, getBitbucketServerDownloadUrl, getBitbucketServerFileFetchUrl, getBitbucketServerRequestOptions, getGerritBranchApiUrl, getGerritCloneRepoUrl, getGerritFileContentsApiUrl, getGerritProjectsApiUrl, getGerritRequestOptions, getGitHubFileFetchUrl, getGitHubRequestOptions, getGitLabFileFetchUrl, getGitLabIntegrationRelativePath, getGitLabRequestOptions, getGiteaFileContentsUrl, getGiteaRequestOptions, getGithubFileFetchUrl, parseGerritGitilesUrl, parseGerritJsonResponse, readAwsS3IntegrationConfig, readAwsS3IntegrationConfigs, readAzureIntegrationConfig, readAzureIntegrationConfigs, readBitbucketCloudIntegrationConfig, readBitbucketCloudIntegrationConfigs, readBitbucketIntegrationConfig, readBitbucketIntegrationConfigs, readBitbucketServerIntegrationConfig, readBitbucketServerIntegrationConfigs, readGerritIntegrationConfig, readGerritIntegrationConfigs, readGitHubIntegrationConfig, readGitHubIntegrationConfigs, readGitLabIntegrationConfig, readGitLabIntegrationConfigs, readGiteaConfig, readGithubIntegrationConfig, readGithubIntegrationConfigs, readGoogleGcsIntegrationConfig, replaceGitHubUrlType, replaceGitLabUrlType, replaceGithubUrlType };
1765
1856
  //# sourceMappingURL=index.esm.js.map