Skip to content

Commit 34bdabd

Browse files
authored
Merge pull request #31 from nitrictech/feature/secret-service
Feature/secret service
2 parents 23120ec + 4bceb98 commit 34bdabd

File tree

8 files changed

+459
-1
lines changed

8 files changed

+459
-1
lines changed

Nitric.Sdk/Nitric.Sdk.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
<Folder Include="Event\" />
3535
<Folder Include="Document\" />
3636
<Folder Include="Proto\" />
37+
<Folder Include="Secret\" />
3738
</ItemGroup>
3839
</Project>

Nitric.Sdk/Secret/Secret.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2021, Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
using System;
15+
using System.Text;
16+
using Nitric.Proto.Secret.v1;
17+
using GrpcClient = Nitric.Proto.Secret.v1.SecretService.SecretServiceClient;
18+
namespace Nitric.Api.Secret
19+
{
20+
public class Secret
21+
{
22+
public readonly static string LATEST = "latest";
23+
24+
internal GrpcClient client;
25+
public readonly string Name;
26+
27+
internal Secret(GrpcClient client, string name)
28+
{
29+
this.client = client;
30+
this.Name = name;
31+
}
32+
public SecretVersion Version(string version)
33+
{
34+
if (string.IsNullOrEmpty(version))
35+
{
36+
throw new ArgumentNullException(version);
37+
}
38+
return new SecretVersion(this, version);
39+
}
40+
public SecretVersion Latest()
41+
{
42+
return new SecretVersion(this, LATEST);
43+
}
44+
public SecretVersion Put(byte[] value)
45+
{
46+
if (value == null || value.Length == 0)
47+
{
48+
throw new ArgumentNullException("provide non-empty value");
49+
}
50+
var request = new SecretPutRequest
51+
{
52+
Secret = new Proto.Secret.v1.Secret { Name = this.Name },
53+
Value = Google.Protobuf.ByteString.CopyFrom(value),
54+
};
55+
var secretResponse = client.Put(request);
56+
return new SecretVersion(
57+
new Secret(
58+
this.client,
59+
secretResponse.SecretVersion.Secret.Name
60+
),
61+
secretResponse.SecretVersion.Version
62+
);
63+
}
64+
public SecretVersion Put(string value)
65+
{
66+
if (string.IsNullOrEmpty(value))
67+
{
68+
throw new ArgumentNullException(value);
69+
}
70+
return Put(Encoding.UTF8.GetBytes(value));
71+
}
72+
public override string ToString()
73+
{
74+
return "[name=" + this.Name + "]";
75+
}
76+
}
77+
}

Nitric.Sdk/Secret/SecretValue.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2021, Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
using System.Text;
15+
namespace Nitric.Api.Secret
16+
{
17+
public class SecretValue
18+
{
19+
public readonly SecretVersion SecretVersion;
20+
public readonly byte[] Value;
21+
public readonly string ValueText;
22+
internal SecretValue(SecretVersion secretVersion, byte[] value)
23+
{
24+
this.SecretVersion = secretVersion;
25+
this.Value = value;
26+
this.ValueText = Encoding.UTF8.GetString(value);
27+
}
28+
public override string ToString()
29+
{
30+
return GetType().Name
31+
+ "[secretVersion=" + this.SecretVersion
32+
+ ", value.length=" + Value.Length
33+
+ "]";
34+
}
35+
}
36+
}

Nitric.Sdk/Secret/SecretVersion.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2021, Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
using System;
15+
using Nitric.Proto.Secret.v1;
16+
namespace Nitric.Api.Secret
17+
{
18+
public class SecretVersion
19+
{
20+
public readonly Secret Secret;
21+
public readonly string Version;
22+
23+
internal SecretVersion(Secret secret, string version)
24+
{
25+
if (secret == null || string.IsNullOrEmpty(secret.Name))
26+
{
27+
throw new ArgumentNullException(secret.Name);
28+
}
29+
if (string.IsNullOrEmpty(version))
30+
{
31+
throw new ArgumentNullException(version);
32+
}
33+
this.Secret = secret;
34+
this.Version = version;
35+
}
36+
public SecretValue Access()
37+
{
38+
var secret = new SecretAccessRequest
39+
{
40+
SecretVersion = new Proto.Secret.v1.SecretVersion
41+
{
42+
Secret = new Proto.Secret.v1.Secret { Name = this.Secret.Name },
43+
Version = this.Version,
44+
}
45+
};
46+
var response = this.Secret.client.Access(secret);
47+
var value = response.Value.ToByteArray();
48+
//Return a new secret value with a reference to this secret version
49+
return new SecretValue(
50+
this,
51+
(value != null && value.Length > 0) ? value : new byte[0]
52+
);
53+
}
54+
public override string ToString()
55+
{
56+
return GetType().Name
57+
+ "[secret=" + this.Secret
58+
+ ", version=" + this.Version
59+
+ "]";
60+
}
61+
}
62+
}

Nitric.Sdk/Secret/Secrets.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2021, Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
using System;
15+
using AbstractClient = Nitric.Api.Common.AbstractClient;
16+
using GrpcClient = Nitric.Proto.Secret.v1.SecretService.SecretServiceClient;
17+
namespace Nitric.Api.Secret
18+
{
19+
public class Secrets : AbstractClient
20+
{
21+
private GrpcClient secretServiceClient;
22+
public Secrets(GrpcClient client = null)
23+
{
24+
this.secretServiceClient = (client != null) ? client : new GrpcClient(this.GetChannel());
25+
}
26+
public Secret Secret(string name)
27+
{
28+
if (string.IsNullOrEmpty(name))
29+
{
30+
throw new ArgumentNullException(name);
31+
}
32+
return new Secret(this.secretServiceClient, name);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)