Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Validate valid ECR URL in refreshECRToken() by calling isECRregistry()
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Rodriguez committed Dec 8, 2023
1 parent a37e285 commit 7413693
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 47 deletions.
19 changes: 14 additions & 5 deletions capabilities/ecr-credential-helper/credential-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Capability } from "pepr";
import { ECRPrivate } from "../ecr-private";
import { ECRPublic } from "../ecr-public";
import { isPrivateECRURL, isPublicECRURL } from "../lib/utils";
import { getZarfRegistryURL, updateZarfManagedImageSecrets } from "../lib/zarf";
import { updateZarfManagedImageSecrets } from "../lib/zarf";
import { isECRregistry } from "../lib/ecr";

/**
* The ECR Credential Helper Capability refreshes ECR tokens for Zarf image pull secrets.
Expand Down Expand Up @@ -37,19 +38,27 @@ async function refreshECRToken(): Promise<void> {
}

try {
const ecrURL = await getZarfRegistryURL();
const result = await isECRregistry();

if (isPrivateECRURL(ecrURL)) {
if (!result.isECR) {
throw new Error(
`A valid ECR URL was not found in the Zarf state secret: ${result.registryURL}\n
Please provide a valid ECR registry URL.\n
Example: '123456789012.dkr.ecr.us-east-1.amazonaws.com'`,
);
}

if (isPrivateECRURL(result.registryURL)) {
const ecrPrivate = new ECRPrivate(region);
authToken = await ecrPrivate.fetchECRToken();
}

if (isPublicECRURL(ecrURL)) {
if (isPublicECRURL(result.registryURL)) {
const ecrPublic = new ECRPublic(region);
authToken = await ecrPublic.fetchECRToken();
}

await updateZarfManagedImageSecrets(ecrURL, authToken);
await updateZarfManagedImageSecrets(result.registryURL, authToken);
} catch (err) {
throw new Error(
`unable to update ECR token in Zarf image pull secrets: ${err}`,
Expand Down
32 changes: 0 additions & 32 deletions capabilities/ecr-webhook/lib/ecr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,6 @@ import { DeployedComponent, ZarfComponent } from "../../zarf-types";
import { privateECRURLPattern, ECRPrivate } from "../../ecr-private";
import { ECRPublic } from "../../ecr-public";
import { isPrivateECRURL, isPublicECRURL } from "../../lib/utils";
import { getZarfRegistryURL } from "../../lib/zarf";

/**
* Represents the result of checking whether the Zarf registry is an ECR registry.
*/
interface ECRCheckResult {
isECR: boolean; // Indicates if the registry is an ECR registry.
registryURL: string; // The URL of the ECR registry.
}

/**
* Check whether the configured Zarf registry is an ECR registry.
* @returns {Promise<ECRCheckResult>} The result of the ECR registry check.
* @throws {Error} If an error occurs while fetching or parsing the Zarf state secret.
*/
export async function isECRregistry(): Promise<ECRCheckResult> {
try {
const registryURL = await getZarfRegistryURL();

if (isPrivateECRURL(registryURL) || isPublicECRURL(registryURL)) {
return { isECR: true, registryURL };
}
} catch (err) {
throw new Error(
`unable to determine if Zarf is configured to use an ECR registry: ${JSON.stringify(
err,
)}`,
);
}

return { isECR: false, registryURL: "" };
}

/**
* Creates ECR repositories for a component in the specified registry.
Expand Down
2 changes: 1 addition & 1 deletion capabilities/ecr-webhook/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Capability, Log, a } from "pepr";
import { isECRregistry } from "./lib/ecr";
import { isECRregistry } from "../lib/ecr";
import { DeployedPackage } from "../zarf-types";
import {
createReposAndUpdateStatus,
Expand Down
34 changes: 34 additions & 0 deletions capabilities/lib/ecr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getZarfRegistryURL } from "./zarf";
import { isPrivateECRURL } from "./utils";
import { isPublicECRURL } from "./utils";

/**
* Represents the result of checking whether the Zarf registry is an ECR registry.
*/
interface ECRCheckResult {
isECR: boolean; // Indicates if the registry is an ECR registry.
registryURL: string; // The URL of the ECR registry.
}

/**
* Check whether the configured Zarf registry is an ECR registry.
* @returns {Promise<ECRCheckResult>} The result of the ECR registry check.
* @throws {Error} If an error occurs while fetching or parsing the Zarf state secret.
*/
export async function isECRregistry(): Promise<ECRCheckResult> {
try {
const registryURL = await getZarfRegistryURL();

if (isPrivateECRURL(registryURL) || isPublicECRURL(registryURL)) {
return { isECR: true, registryURL };
}
} catch (err) {
throw new Error(
`unable to determine if Zarf is configured to use an ECR registry: ${JSON.stringify(
err,
)}`,
);
}

return { isECR: false, registryURL: "" };
}
18 changes: 9 additions & 9 deletions manifests/pepr-module-b95dbd80-e078-5eb9-aaf3-bcb9567417d0.yaml

Large diffs are not rendered by default.

0 comments on commit 7413693

Please sign in to comment.