secret function

Future<String> secret(
  1. String name
)

Retrieves a secret from the platform's secret manager.

If the secret is exposed in an environment variable with the same name, the secret value is directly returned from there. If not, this function queries Google Cloud Secret Manager.

Implementation

Future<String> secret(String name) async {
  final envSecret = Platform.environment[name];
  if (envSecret != null) {
    return envSecret;
  }

  final client = await clientViaMetadataServer();

  final secretManagerApi = SecretManagerApi(client);
  final secretPath =
      'projects/${await projectId}/secrets/$name/versions/latest';

  final response = await secretManagerApi.projects.secrets.versions.access(
    secretPath,
  );
  return String.fromCharCodes(response.payload!.dataAsBytes);
}