-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgetRegistryCredentialsAndSecrets.ts
More file actions
66 lines (56 loc) · 2.95 KB
/
getRegistryCredentialsAndSecrets.ts
File metadata and controls
66 lines (56 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { RegistryCredentials, Secret } from "@azure/arm-appcontainers";
import { nonNullProp } from "@microsoft/vscode-azext-utils";
import { dockerHubDomain, dockerHubRegistry } from "../../../../constants";
import type { IContainerRegistryImageContext } from "./IContainerRegistryImageContext";
import { listCredentialsFromRegistry } from "./acr/listCredentialsFromRegistry";
interface RegistryCredentialsAndSecrets {
registries?: RegistryCredentials[];
secrets?: Secret[];
}
export async function getAcrCredentialsAndSecrets(context: IContainerRegistryImageContext, containerAppSettings?: RegistryCredentialsAndSecrets): Promise<RegistryCredentialsAndSecrets> {
const registry = nonNullProp(context, 'registry');
const { username, password } = await listCredentialsFromRegistry(context, registry);
const passwordName = `${registry.name?.toLocaleLowerCase()}-${password?.name}`;
// Remove duplicate registries
const registries: RegistryCredentials[] = containerAppSettings?.registries?.filter(r => r.server !== registry.loginServer) ?? [];
registries?.push(
{
identity: '',
server: registry.loginServer,
username: username,
passwordSecretRef: passwordName
}
);
// Remove duplicate secrets
const secrets: Secret[] = containerAppSettings?.secrets?.filter(s => s.name !== passwordName) ?? [];
secrets?.push({ name: passwordName, value: password.value });
return { registries, secrets };
}
export function getThirdPartyCredentialsAndSecrets(context: IContainerRegistryImageContext, containerAppSettings?: RegistryCredentialsAndSecrets): RegistryCredentialsAndSecrets {
// If 'docker.io', convert to 'index.docker.io', else use registryName as loginServer
const loginServer: string = (context.registryDomain === dockerHubDomain) ? dockerHubRegistry : nonNullProp(context, 'registryName').toLowerCase();
const passwordSecretRef: string = `${loginServer.replace(/[\.]+/g, '')}-${context.username}`;
// Remove duplicate registries
const registries: RegistryCredentials[] = containerAppSettings?.registries?.filter(r => r.server !== loginServer) ?? [];
registries?.push(
{
identity: '',
server: loginServer,
username: context.username,
passwordSecretRef
}
);
// Remove duplicate secrets
const secrets: Secret[] = containerAppSettings?.secrets?.filter(s => s.name !== passwordSecretRef) ?? [];
secrets?.push(
{
name: passwordSecretRef,
value: context.secret
}
);
return { registries, secrets };
}