migrate AWS config to authMetadata

and remove specialized AWSClusterDetails type accordingly

Signed-off-by: Jamie Klassen <jklassen@vmware.com>
This commit is contained in:
Jamie Klassen
2023-09-05 16:02:39 -04:00
parent 094b12b59e
commit 7982094539
8 changed files with 51 additions and 56 deletions
@@ -111,8 +111,10 @@ describe('CatalogClusterLocator', () => {
url: 'https://apiserver.com',
caData: 'caData',
authProvider: 'aws',
assumeRole: 'my-role',
externalId: 'my-id',
authMetadata: {
assumeRole: 'my-role',
externalId: 'my-id',
},
oidcTokenProvider: 'google',
skipMetricsLookup: false,
skipTLSVerify: false,
@@ -14,11 +14,7 @@
* limitations under the License.
*/
import {
AWSClusterDetails,
ClusterDetails,
KubernetesClustersSupplier,
} from '../types/types';
import { ClusterDetails, KubernetesClustersSupplier } from '../types/types';
import { CATALOG_FILTER_EXISTS, CatalogApi } from '@backstage/catalog-client';
import {
ANNOTATION_KUBERNETES_API_SERVER,
@@ -89,15 +85,17 @@ export class CatalogClusterLocator implements KubernetesClustersSupplier {
if (clusterDetails.authProvider === 'aws') {
return {
...clusterDetails,
assumeRole:
entity.metadata.annotations![
ANNOTATION_KUBERNETES_AWS_ASSUME_ROLE
]!,
externalId:
entity.metadata.annotations![
ANNOTATION_KUBERNETES_AWS_EXTERNAL_ID
]!,
} as AWSClusterDetails;
authMetadata: {
assumeRole:
entity.metadata.annotations![
ANNOTATION_KUBERNETES_AWS_ASSUME_ROLE
]!,
externalId:
entity.metadata.annotations![
ANNOTATION_KUBERNETES_AWS_EXTERNAL_ID
]!,
},
};
}
return clusterDetails;
@@ -17,7 +17,7 @@
import '@backstage/backend-common';
import { ConfigReader, Config } from '@backstage/config';
import { ConfigClusterLocator } from './ConfigClusterLocator';
import { AWSClusterDetails, ClusterDetails } from '../types/types';
import { ClusterDetails } from '../types/types';
describe('ConfigClusterLocator', () => {
it('empty clusters returns empty cluster details', async () => {
@@ -142,36 +142,39 @@ describe('ConfigClusterLocator', () => {
const result = await sut.getClusters();
expect(result).toStrictEqual<AWSClusterDetails[]>([
expect(result).toStrictEqual<ClusterDetails[]>([
{
assumeRole: undefined,
name: 'cluster1',
externalId: undefined,
url: 'http://localhost:8080',
authProvider: 'aws',
authMetadata: { serviceAccountToken: 'token' },
authMetadata: {
serviceAccountToken: 'token',
},
skipTLSVerify: false,
skipMetricsLookup: false,
caData: undefined,
caFile: undefined,
},
{
assumeRole: 'SomeRole',
name: 'cluster2',
externalId: undefined,
url: 'http://localhost:8081',
authProvider: 'aws',
authMetadata: {
assumeRole: 'SomeRole',
},
skipTLSVerify: true,
skipMetricsLookup: false,
caData: undefined,
caFile: undefined,
},
{
assumeRole: 'SomeRole',
name: 'cluster2',
externalId: 'SomeExternalId',
url: 'http://localhost:8081',
authProvider: 'aws',
authMetadata: {
assumeRole: 'SomeRole',
externalId: 'SomeExternalId',
},
skipTLSVerify: true,
skipMetricsLookup: false,
caData: undefined,
@@ -73,7 +73,14 @@ export class ConfigClusterLocator implements KubernetesClustersSupplier {
const assumeRole = c.getOptionalString('assumeRole');
const externalId = c.getOptionalString('externalId');
return { assumeRole, externalId, ...clusterDetails };
return {
authMetadata: {
...(assumeRole && { assumeRole }),
...(externalId && { externalId }),
...clusterDetails.authMetadata,
},
...clusterDetails,
};
}
case 'azure': {
return clusterDetails;
@@ -70,10 +70,10 @@ describe('AwsIamKubernetesAuthTranslator tests', () => {
const authTranslator = new AwsIamKubernetesAuthTranslator({ config });
const authPromise = authTranslator.decorateClusterDetailsWithAuth({
assumeRole: 'SomeRole',
name: 'test-cluster',
url: '',
authProvider: 'aws',
authMetadata: { assumeRole: 'SomeRole' },
});
expect((await authPromise).authMetadata!.serviceAccountToken).toEqual(
'k8s-aws-v1.aHR0cHM6Ly9odHRwczovL2V4YW1wbGUuY29tL2FzZGY_',
@@ -96,11 +96,13 @@ describe('AwsIamKubernetesAuthTranslator tests', () => {
const authTranslator = new AwsIamKubernetesAuthTranslator({ config });
const authPromise = authTranslator.decorateClusterDetailsWithAuth({
assumeRole: 'SomeRole',
externalId: 'external-id',
name: 'test-cluster',
url: '',
authProvider: 'aws',
authMetadata: {
assumeRole: 'SomeRole',
externalId: 'external-id',
},
});
expect((await authPromise).authMetadata!.serviceAccountToken).toEqual(
'k8s-aws-v1.aHR0cHM6Ly9odHRwczovL2V4YW1wbGUuY29tL2FzZGY_',
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AWSClusterDetails } from '../types/types';
import { ClusterDetails } from '../types/types';
import { KubernetesAuthTranslator } from './types';
import { fromTemporaryCredentials } from '@aws-sdk/credential-providers';
import { SignatureV4 } from '@aws-sdk/signature-v4';
@@ -110,9 +110,9 @@ export class AwsIamKubernetesAuthTranslator
}
async decorateClusterDetailsWithAuth(
clusterDetails: AWSClusterDetails,
): Promise<AWSClusterDetails> {
const clusterDetailsWithAuthToken: AWSClusterDetails = Object.assign(
clusterDetails: ClusterDetails,
): Promise<ClusterDetails> {
const clusterDetailsWithAuthToken: ClusterDetails = Object.assign(
{},
clusterDetails,
);
@@ -120,8 +120,8 @@ export class AwsIamKubernetesAuthTranslator
clusterDetailsWithAuthToken.authMetadata = {
serviceAccountToken: await this.getBearerToken(
clusterDetails.name,
clusterDetails.assumeRole,
clusterDetails.externalId,
clusterDetails.authMetadata?.assumeRole,
clusterDetails.authMetadata?.externalId,
),
...clusterDetailsWithAuthToken.authMetadata,
};
+1 -10
View File
@@ -33,7 +33,7 @@ import { Config } from '@backstage/config';
*/
export interface ObjectFetchParams {
serviceId: string;
clusterDetails: AWSClusterDetails | GKEClusterDetails | ClusterDetails;
clusterDetails: GKEClusterDetails | ClusterDetails;
objectTypesToFetch: Set<ObjectToFetch>;
labelSelector: string;
customResources: CustomResource[];
@@ -220,15 +220,6 @@ export interface GKEClusterDetails extends ClusterDetails {}
*/
export interface AzureClusterDetails extends ClusterDetails {}
/**
*
* @public
*/
export interface AWSClusterDetails extends ClusterDetails {
assumeRole?: string;
externalId?: string;
}
/**
*
* @public