Example #1
0
        private async Task <string> AccessSecretVersion(string secretId, string secretVersion)
        {
            m_logger.Trace($"Attempting to fetch version {secretVersion} of secret {secretId}");
            var request = new AccessSecretVersionRequest
            {
                SecretVersionName = new SecretVersionName(m_projectId, secretId, secretVersion),
            };

            var response = await m_client.AccessSecretVersionAsync(request);

            m_logger.Trace($"Fetched version {secretVersion} of secret {secretId}");
            return(response.Payload.Data.ToStringUtf8());
        }
        public async Task <String> getSecretManagerValues(String project_id, String secret_id, String secret_version_id)
        {
            client = await SecretManagerServiceClient.CreateAsync();

            // Create the request.
            var request = new AccessSecretVersionRequest
            {
                SecretVersionName = new SecretVersionName(project_id, secret_id, secret_version_id),
            };

            // Access the secret and print the result.
            //
            var version = await client.AccessSecretVersionAsync(request);

            string payload = version.Payload.Data.ToStringUtf8();

            return(payload);
        }
        // [START secretmanager_access_secret_version]
        /// <summary>
        /// Accesses a secret with provided version.
        /// </summary>
        /// <param name="projectId">ID of the project where the secret resides.</param>
        /// <param name="secretId">ID of the secret.</param>
        /// <param name="secretVersion">Version of the secret.</param>
        /// <example>
        /// With a specific version.
        /// <code>AccessSecretVersion("my-project", "my-secret", "5")</code>
        /// </example>
        /// <example>
        /// With an alias version.
        /// <code>AccessSecretVersion("my-project", "my-secret", "latest")</code>
        /// </example>
        public static void AccessSecretVersion(string projectId, string secretId, string secretVersion)
        {
            SecretManagerServiceClient client = SecretManagerServiceClient.Create();

            // Create the request.
            var request = new AccessSecretVersionRequest
            {
                SecretVersionName = new SecretVersionName(projectId, secretId, secretVersion),
            };

            // Access the secret and print the result.
            //
            // WARNING: Do not print secrets in production environments. This
            // snippet is for demonstration purposes only.
            var    version = client.AccessSecretVersion(request);
            string payload = version.Payload.Data.ToStringUtf8();

            Console.WriteLine($"Payload: {payload}");
        }
Example #4
0
        public static void Main(string[] args)
        {
            // GCP project in which to store secrets in Secret Manager.
            string projectId = "YOUR-PROJECT-ID";

            // ID of the secret to create.
            string secretId = "YOUR-SECRET-ID";

            // [END secretmanager_quickstart]
            if (args.Length > 1)
            {
                projectId = args[0];
                secretId  = args[1];
            }
            // [START secretmanager_quickstart]
            // Create a Secret Manager client.
            SecretManagerServiceClient client = SecretManagerServiceClient.Create();

            // Create the parent secret.
            var createSecretRequest = new CreateSecretRequest
            {
                ParentAsProjectName = new ProjectName(projectId),
                SecretId            = secretId,
                Secret = new Secret
                {
                    Replication = new Replication
                    {
                        Automatic = new Replication.Types.Automatic(),
                    },
                },
            };

            var secret = client.CreateSecret(createSecretRequest);

            // Add a secret version.
            var addSecretVersionRequest = new AddSecretVersionRequest
            {
                ParentAsSecretName = secret.SecretName,
                Payload            = new SecretPayload
                {
                    Data = ByteString.CopyFrom("my super secret data", Encoding.UTF8),
                },
            };

            var version = client.AddSecretVersion(addSecretVersionRequest);

            // Access the secret version.
            var accessSecretVersionRequest = new AccessSecretVersionRequest
            {
                SecretVersionName = version.SecretVersionName,
            };

            var result = client.AccessSecretVersion(accessSecretVersionRequest);

            // Print the results
            //
            // WARNING: Do not print secrets in production environments. This
            // snippet is for demonstration purposes only.
            string payload = result.Payload.Data.ToStringUtf8();

            Console.WriteLine($"Plaintext: {payload}");
        }