VerticalRelevance / aws-github-oidc

An OpenID Connect provider, and an Action to deploy it to an account, to enable OIDC authentication from GitHub to AWS.
Apache License 2.0
0 stars 0 forks source link

Internalize the SDK call listing providers #15

Open douglasnaphas opened 1 year ago

douglasnaphas commented 1 year ago

Instead of using it like this:

  const response2GitHubProviderArn =
  require("aws-github-oidc").response2GitHubProviderArn;

 // ...

  // Check for a GitHub OIDC Provider
  const client = new IAMClient({ region });
  const input = {};
  const command = new ListOpenIDConnectProvidersCommand(input);
  let response;
  try {
    response = await client.send(command);
  } catch (error: any) {
    if (error.Code === "ExpiredToken") {
      console.error(
        "expired token, try setting the variables AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN to valid credentials"
      );
      const EXPIRED_TOKEN = 2;
      process.exit(EXPIRED_TOKEN);
    }
    console.log("error listing OpenID Connect Providers");
    const ERROR_LISTING_PROVIDERS = 4;
    process.exit(ERROR_LISTING_PROVIDERS);
  }
  const BAD_RESPONSE = 3;
  if (!response) {
    process.exit(BAD_RESPONSE);
  }

  const providerArn = response2GitHubProviderArn(response) || "";
  if (providerArn === "") {
    const NO_GITHUB_PROVIDER = 5;
    console.error("No GitHub Provider");
    process.exit(NO_GITHUB_PROVIDER);
  }

we want to use it like this:

import {getGitHubOIDCProviderArn} from "aws-github-oidc";

// ...

try{
  const providerArn = getGitHubOIDCProviderArn();
} catch(err) {
  console.error("Unable to find a GitHub OIDC Provider in this account. Try creating one from the CLI using https://github.com/VerticalRelevance/aws-github-oidc.");
  process.exit(1);
}

Maybe also export specific error objects, so that they can be inspected and error messages can be tailored.