Exemplo n.º 1
0
        public static async Task Main(string[] args)
        {
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

            SecretManagerServiceClient client = SecretManagerServiceClient.Create();
            var json = client.AccessSecretVersion(new SecretVersionName(ProjectId, "agones-allocator-info", "1")).Payload.Data.ToStringUtf8();
            var agonesAllocatorInfo = JsonConvert.DeserializeObject <AgonesAllocatorInfo>(json);

            while (true)
            {
                try {
                    List <Match> matches = await WaitingMatchAsync();

                    foreach (var match in matches)
                    {
                        await Task.Run(async() =>
                        {
                            var allocationResponse = await AllocateGameServerAsync(agonesAllocatorInfo);
                            await AssignTicketAsync(allocationResponse, match);
                        });
                    }
                } catch (Exception e) {
                    Console.WriteLine(e);
                } finally {
                    await Task.Delay(TaskDealyMillisecondsDelay);
                }
            }
        }
Exemplo n.º 2
0
    public Policy IamGrantAccess(
        string projectId = "my-project", string secretId = "my-secret",
        string member    = "user:[email protected]")
    {
        // Create the client.
        SecretManagerServiceClient client = SecretManagerServiceClient.Create();

        // Build the resource name.
        SecretName secretName = new SecretName(projectId, secretId);

        // Get current policy.
        Policy policy = client.GetIamPolicy(new GetIamPolicyRequest
        {
            ResourceAsResourceName = secretName,
        });

        // Add the user to the list of bindings.
        policy.AddRoleMember("roles/secretmanager.secretAccessor", member);

        // Save the updated policy.
        policy = client.SetIamPolicy(new SetIamPolicyRequest
        {
            ResourceAsResourceName = secretName,
            Policy = policy,
        });
        return(policy);
    }
Exemplo n.º 3
0
        // [END secretmanager_iam_grant_access]

        // [START secretmanager_iam_revoke_access]
        /// <summary>
        /// Revoke a user or account access to the secret.
        /// </summary>
        /// <param name="projectId">ID of the project where the secret resides.</param>
        /// <param name="secretId">ID of the secret.</param>
        /// <param name="member">IAM member to revoke with user: or serviceAccount: prefix</param>
        /// <example>
        /// Revoke a user or account access to the secret.
        /// <code>IAMRevokeAccess("my-project", "my-secret", "user:[email protected]")</code>
        /// </example>
        public static void IAMRevokeAccess(string projectId, string secretId, string member)
        {
            SecretManagerServiceClient client = SecretManagerServiceClient.Create();

            // Create the request to get the current IAM policy.
            var getRequest = new GetIamPolicyRequest
            {
                ResourceAsResourceName = new SecretName(projectId, secretId),
            };

            // Get the current IAM policy.
            var policy = client.GetIamPolicy(getRequest);

            // Remove the user to the list of bindings.
            policy.RemoveRoleMember("roles/secretmanager.secretAccessor", member);

            // Create the request to update the IAM policy.
            var setRequest = new SetIamPolicyRequest
            {
                ResourceAsResourceName = new SecretName(projectId, secretId),
                Policy = policy,
            };

            // Save the updated IAM policy.
            client.SetIamPolicy(setRequest);

            Console.WriteLine($"Updated IAM policy for {secretId}");
        }
Exemplo n.º 4
0
        public void Delete(string id)
        {
            var client = SecretManagerServiceClient.Create();

            var secretName = new SecretName(ProjectId, id);

            client.DeleteSecret(secretName);
        }
 public void CreateClient()
 {
     if (SecretManagerClient == null)
     {
         _logger.LogDebug($"Creating secret manager client ...");
         SecretManagerClient = SecretManagerServiceClient.Create();
     }
 }
Exemplo n.º 6
0
        public IActionResult AccessVersion(string id, string versionId)
        {
            var client = SecretManagerServiceClient.Create();

            var secretVersionName = new SecretVersionName(ProjectId, id, versionId);

            return(Ok(client.AccessSecretVersion(secretVersionName).Payload.Data.ToStringUtf8()));
        }
Exemplo n.º 7
0
        public IActionResult GetVersion(string id, string versionId)
        {
            var client = SecretManagerServiceClient.Create();

            var secretVersionName = new SecretVersionName(ProjectId, id, versionId);

            return(Ok(client.GetSecretVersion(secretVersionName)));
        }
Exemplo n.º 8
0
        public void DeleteVersion(string id, string versionId)
        {
            var client = SecretManagerServiceClient.Create();

            var secretVersionName = new SecretVersionName(ProjectId, id, versionId);

            client.DestroySecretVersion(secretVersionName);
        }
    public void DeletesSecrets()
    {
        SecretName secretName = _fixture.SecretToDelete.SecretName;

        _sample.DeleteSecret(projectId: secretName.ProjectId, secretId: secretName.SecretId);

        SecretManagerServiceClient client = SecretManagerServiceClient.Create();

        Assert.Throws <Grpc.Core.RpcException>(() => client.GetSecret(secretName));
    }
Exemplo n.º 10
0
    private SecretVersion AddSecretVersion(Secret secret)
    {
        SecretManagerServiceClient client = SecretManagerServiceClient.Create();

        SecretPayload payload = new SecretPayload
        {
            Data = ByteString.CopyFrom("my super secret data", Encoding.UTF8),
        };

        return(client.AddSecretVersion(secret.SecretName, payload));
    }
Exemplo n.º 11
0
    public void DeleteSecret(
        string projectId = "my-project", string secretId = "my-secret")
    {
        // Create the client.
        SecretManagerServiceClient client = SecretManagerServiceClient.Create();

        // Build the resource name.
        SecretName secretName = new SecretName(projectId, secretId);

        // Delete the secret.
        client.DeleteSecret(secretName);
    }
Exemplo n.º 12
0
    public void AddsSecretVersions()
    {
        string        data          = "my secret data";
        SecretName    secretName    = _fixture.Secret.SecretName;
        SecretVersion secretVersion = _sample.AddSecretVersion(
            projectId: secretName.ProjectId, secretId: secretName.SecretId,
            data: data);

        SecretManagerServiceClient  client = SecretManagerServiceClient.Create();
        AccessSecretVersionResponse result = client.AccessSecretVersion(secretVersion.SecretVersionName);

        Assert.Equal(data, result.Payload.Data.ToStringUtf8());
    }
Exemplo n.º 13
0
    private void DeleteSecret(SecretName name)
    {
        SecretManagerServiceClient client = SecretManagerServiceClient.Create();

        try
        {
            client.DeleteSecret(name);
        }
        catch (Grpc.Core.RpcException e) when(e.StatusCode == Grpc.Core.StatusCode.NotFound)
        {
            // Ignore error - secret was already deleted
        }
    }
Exemplo n.º 14
0
    public Secret GetSecret(string projectId = "my-project", string secretId = "my-secret")
    {
        // Create the client.
        SecretManagerServiceClient client = SecretManagerServiceClient.Create();

        // Build the resource name.
        SecretName secretName = new SecretName(projectId, secretId);

        // Call the API.
        Secret secret = client.GetSecret(secretName);

        return(secret);
    }
Exemplo n.º 15
0
        public IActionResult PostVersion(string id, [FromBody] string data)
        {
            var client = SecretManagerServiceClient.Create();

            var secretName = new SecretName(ProjectId, id);

            var payload = new SecretPayload
            {
                Data = ByteString.CopyFrom(data, Encoding.UTF8),
            };

            return(Ok(client.AddSecretVersion(secretName, payload)));
        }
Exemplo n.º 16
0
        public static DbSettings GetDbSettingsFromSecrets(string projectId, string secretName)
        {
            var client = SecretManagerServiceClient.Create();

            var secretValue = client.AccessSecretVersion(new AccessSecretVersionRequest()
            {
                SecretVersionName = new SecretVersionName(projectId, secretName, "latest")
            });

            var data       = secretValue.Payload.Data.ToStringUtf8();
            var dbSettings = Newtonsoft.Json.JsonConvert.DeserializeObject <DbSettings>(data);

            return(dbSettings);
        }
Exemplo n.º 17
0
    public void ListSecrets(string projectId = "my-project")
    {
        // Create the client.
        SecretManagerServiceClient client = SecretManagerServiceClient.Create();

        // Build the resource name.
        ProjectName projectName = new ProjectName(projectId);

        // Call the API.
        foreach (Secret secret in client.ListSecrets(projectName))
        {
            // ...
        }
    }
Exemplo n.º 18
0
    public SecretVersion GetSecretVersion(
        string projectId = "my-project", string secretId = "my-secret", string secretVersionId = "123")
    {
        // Create the client.
        SecretManagerServiceClient client = SecretManagerServiceClient.Create();

        // Build the resource name.
        SecretVersionName secretVersionName = new SecretVersionName(projectId, secretId, secretVersionId);

        // Call the API.
        SecretVersion version = client.GetSecretVersion(secretVersionName);

        return(version);
    }
Exemplo n.º 19
0
        // [END secretmanager_create_secret]

        // [START secretmanager_delete_secret]
        /// <summary>
        /// Delete an existing secret with the given name.
        /// </summary>
        /// <param name="projectId">ID of the project where the secret resides.</param>
        /// <param name="secretId">ID of the secret.</param>
        /// <example>
        /// Delete a secret.
        /// <code>DeleteSecret("my-project", "my-secret")</code>
        /// </example>
        public static void DeleteSecret(string projectId, string secretId)
        {
            SecretManagerServiceClient client = SecretManagerServiceClient.Create();

            // Create the request.
            var request = new DeleteSecretRequest
            {
                SecretName = new SecretName(projectId, secretId),
            };

            // Delete the secret.
            client.DeleteSecret(request);
            Console.WriteLine($"Deleted secret {secretId}");
        }
    public void ListSecretVersions(string projectId = "my-project", string secretId = "my-secret")
    {
        // Create the client.
        SecretManagerServiceClient client = SecretManagerServiceClient.Create();

        // Build the resource name.
        SecretName secretName = new SecretName(projectId, secretId);

        // Call the API.
        foreach (SecretVersion secretVersion in client.ListSecretVersions(secretName))
        {
            // ...
        }
    }
Exemplo n.º 21
0
        // [END secretmanager_enable_secret_version]

        // [START secretmanager_get_secret_version]
        /// <summary>
        /// Get an existing secret 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>
        /// Get an existing secret version.
        /// <code>GetSecretVersion("my-project", "my-secret", "5")</code>
        /// </example>
        /// <example>
        /// With an alias version.
        /// <code>GetSecretVersion("my-project", "my-secret", "latest")</code>
        /// </example>
        public static void GetSecretVersion(string projectId, string secretId, string secretVersion)
        {
            SecretManagerServiceClient client = SecretManagerServiceClient.Create();

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

            // Get the secret version.
            var version = client.GetSecretVersion(request);

            Console.WriteLine($"Secret version {version.Name}, state {version.State}");
        }
Exemplo n.º 22
0
        // [END secretmanager_get_secret_version]

        // [START secretmanager_get_secret]
        /// <summary>
        /// Get an existing secret.
        /// </summary>
        /// <param name="projectId">ID of the project where the secret resides.</param>
        /// <param name="secretId">ID of the secret.</param>
        /// <example>
        /// Get an existing secret.
        /// <code>GetSecret("my-project", "my-secret")</code>
        /// </example>
        public static void GetSecret(string projectId, string secretId)
        {
            SecretManagerServiceClient client = SecretManagerServiceClient.Create();

            // Create the request.
            var request = new GetSecretRequest
            {
                SecretName = new SecretName(projectId, secretId),
            };

            // Get the secret.
            var secret = client.GetSecret(request);

            Console.WriteLine($"Secret {secret.Name}, replication {secret.Replication.ReplicationCase}");
        }
Exemplo n.º 23
0
    public Secret CreateSecret(string secretId)
    {
        SecretManagerServiceClient client = SecretManagerServiceClient.Create();
        ProjectName projectName           = new ProjectName(ProjectId);

        Secret secret = new Secret
        {
            Replication = new Replication
            {
                Automatic = new Replication.Types.Automatic(),
            },
        };

        return(client.CreateSecret(projectName, secretId, secret));
    }
Exemplo n.º 24
0
        /// <summary>
        /// GetSecretValue
        /// Retrieve latest secret value
        /// </summary>
        /// <param name="secretKey"></param>
        /// <returns></returns>
        public string GetSecretValue(string secretKey)
        {
            // Create the client.
            SecretManagerServiceClient client = SecretManagerServiceClient.Create();

            // Build the resource name.
            SecretVersionName secretVersionName = new SecretVersionName(_configuration.GcpProjectId, secretKey, "latest");

            // Call the API.
            AccessSecretVersionResponse result = client.AccessSecretVersion(secretVersionName);

            // Convert the payload to a string. Payloads are bytes by default.
            string payload = result.Payload.Data.ToStringUtf8();

            return(payload);
        }
Exemplo n.º 25
0
        public void Post([FromBody] string secretId)
        {
            var client = SecretManagerServiceClient.Create();

            var projectName = new ProjectName(ProjectId);

            var secret = new Secret
            {
                Replication = new Replication
                {
                    Automatic = new Replication.Types.Automatic(),
                },
            };

            client.CreateSecret(projectName, secretId, secret);
        }
Exemplo n.º 26
0
        // [END secretmanager_list_secret_versions]

        // [START secretmanager_list_secrets]
        /// <summary>
        /// List all secret for a project
        /// </summary>
        /// <param name="projectId">ID of the project where secrets reside.</param>
        /// <example>
        /// List all secrets.
        /// <code>ListSecrets("my-project")</code>
        /// </example>
        public static void ListSecrets(string projectId)
        {
            SecretManagerServiceClient client = SecretManagerServiceClient.Create();

            // Create the request.
            var request = new ListSecretsRequest
            {
                ParentAsProjectName = new ProjectName(projectId),
            };

            // List all secrets in the project.
            foreach (var secret in client.ListSecrets(request))
            {
                Console.WriteLine($"Secret {secret.Name}");
            }
        }
Exemplo n.º 27
0
        // [END secretmanager_get_secret]

        // [START secretmanager_list_secret_versions]
        /// <summary>
        /// List all secret versions for a secret.
        /// </summary>
        /// <param name="projectId">ID of the project where the secret resides.</param>
        /// <param name="secretId">ID of the secret.</param>
        /// <example>
        /// List all secret versions.
        /// <code>ListSecretVersions("my-project", "my-secret")</code>
        /// </example>
        public static void ListSecretVersions(string projectId, string secretId)
        {
            SecretManagerServiceClient client = SecretManagerServiceClient.Create();

            // Create the request.
            var request = new ListSecretVersionsRequest
            {
                ParentAsSecretName = new SecretName(projectId, secretId),
            };

            // List all versions and their state.
            foreach (var version in client.ListSecretVersions(request))
            {
                Console.WriteLine($"Secret version {version.Name}, {version.State}");
            }
        }
Exemplo n.º 28
0
    public String AccessSecretVersion(
        string projectId = "my-project", string secretId = "my-secret", string secretVersionId = "123")
    {
        // Create the client.
        SecretManagerServiceClient client = SecretManagerServiceClient.Create();

        // Build the resource name.
        SecretVersionName secretVersionName = new SecretVersionName(projectId, secretId, secretVersionId);

        // Call the API.
        AccessSecretVersionResponse result = client.AccessSecretVersion(secretVersionName);

        // Convert the payload to a string. Payloads are bytes by default.
        String payload = result.Payload.Data.ToStringUtf8();

        return(payload);
    }
Exemplo n.º 29
0
        string GetDBPassword()
        {
            // Create the client.
            SecretManagerServiceClient client = SecretManagerServiceClient.Create();

            // Build the resource name.
            SecretVersionName secretVersionName = new SecretVersionName(
                Environment.GetEnvironmentVariable("PROJECT"),
                Environment.GetEnvironmentVariable("SECRET_ID"),
                Environment.GetEnvironmentVariable("SECRET_VER"));

            // Call the API.
            AccessSecretVersionResponse result = client.AccessSecretVersion(secretVersionName);

            // Convert the payload to a string. Payloads are bytes by default.
            return(result.Payload.Data.ToStringUtf8());
        }
Exemplo n.º 30
0
        private string GetDBUserName()
        {
            // Create the client.
            SecretManagerServiceClient client = SecretManagerServiceClient.Create();

            // Build the resource name.
            SecretVersionName secretVersionName = new SecretVersionName(
                "dev-office-294516",
                "db-user-name",
                "1");

            // Call the API.
            AccessSecretVersionResponse result = client.AccessSecretVersion(secretVersionName);

            // Convert the payload to a string. Payloads are bytes by default.
            return(result.Payload.Data.ToStringUtf8());
        }