-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from nitrictech/feature/secret-local
Add local dev secret plugin
- Loading branch information
Showing
6 changed files
with
304 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright 2021 Nitric Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package secret_service | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
"github.com/nitric-dev/membrane/pkg/plugins/secret" | ||
"github.com/nitric-dev/membrane/pkg/utils" | ||
) | ||
|
||
const DEFAULT_DIR = ".nitric/secrets/" | ||
|
||
type DevSecretService struct { | ||
secret.UnimplementedSecretPlugin | ||
secDir string | ||
} | ||
|
||
func (s *DevSecretService) secretFileName(sec *secret.Secret, v string) string { | ||
return fmt.Sprintf("%s/%s_%s.txt", s.secDir, sec.Name, v) | ||
} | ||
|
||
func (s *DevSecretService) Put(sec *secret.Secret, val []byte) (*secret.SecretPutResponse, error) { | ||
if sec == nil { | ||
return nil, fmt.Errorf("provide non-empty secret") | ||
} | ||
if len(sec.Name) == 0 { | ||
return nil, fmt.Errorf("provide non-blank secret name") | ||
} | ||
if len(val) == 0 { | ||
return nil, fmt.Errorf("provide non-blank secret value") | ||
} | ||
|
||
var versionId = uuid.New().String() | ||
//Creates a new file in the form: | ||
// DIR/Name_Version.txt | ||
file, err := os.Create(s.secretFileName(sec, versionId)) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating secret store: %v", err) | ||
} | ||
writer := bufio.NewWriter(file) | ||
writer.WriteString(string(val)) | ||
writer.Flush() | ||
|
||
//Creates a new file as latest | ||
latestFile, err := os.Create(s.secretFileName(sec, "latest")) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating latest secret: %v", err) | ||
} | ||
latestWriter := bufio.NewWriter(latestFile) | ||
latestWriter.WriteString(string(val)) | ||
latestWriter.WriteString("," + versionId) | ||
latestWriter.Flush() | ||
|
||
return &secret.SecretPutResponse{ | ||
SecretVersion: &secret.SecretVersion{ | ||
Secret: &secret.Secret{ | ||
Name: sec.Name, | ||
}, | ||
Version: versionId, | ||
}, | ||
}, nil | ||
} | ||
|
||
func (s *DevSecretService) Access(sv *secret.SecretVersion) (*secret.SecretAccessResponse, error) { | ||
if sv.Secret.Name == "" { | ||
return nil, fmt.Errorf("provide non-blank name") | ||
} | ||
if sv.Version == "" { | ||
return nil, fmt.Errorf("provide non-blank version") | ||
} | ||
|
||
content, err := ioutil.ReadFile(s.secretFileName(sv.Secret, sv.Version)) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading secret store: %v", err) | ||
} | ||
|
||
splitContent := strings.Split(string(content), ",") | ||
return &secret.SecretAccessResponse{ | ||
SecretVersion: &secret.SecretVersion{ | ||
Secret: &secret.Secret{ | ||
Name: sv.Secret.Name, | ||
}, | ||
Version: splitContent[len(splitContent)-1], | ||
}, | ||
Value: []byte(splitContent[0]), | ||
}, nil | ||
} | ||
|
||
//Create new secret store | ||
func New() (secret.SecretService, error) { | ||
secDir := utils.GetEnv("LOCAL_SEC_DIR", DEFAULT_DIR) | ||
|
||
//Check whether file exists | ||
_, err := os.Stat(secDir) | ||
if os.IsNotExist(err) { | ||
//Make directory if not present | ||
err := os.MkdirAll(secDir, 0777) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return &DevSecretService{ | ||
secDir: secDir, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2021 Nitric Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package secret_service_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestPubsub(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Dev Secret Plugin Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright 2021 Nitric Pty Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package secret_service_test | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/nitric-dev/membrane/pkg/plugins/secret" | ||
secretPlugin "github.com/nitric-dev/membrane/pkg/plugins/secret/dev" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Dev Secret Manager", func() { | ||
os.Setenv("LOCAL_SEC_DIR", "./.nitric/") | ||
|
||
AfterSuite(func() { | ||
// Cleanup default secrect directory | ||
os.RemoveAll("./.nitric/") | ||
}) | ||
|
||
testSecret := secret.Secret{ | ||
Name: "Test", | ||
} | ||
testSecretVal := []byte("Super Secret Message") | ||
When("Put", func() { | ||
When("Putting a secret to a non-existent secret", func() { | ||
secretPlugin, _ := secretPlugin.New() | ||
It("Should successfully store a secret", func() { | ||
response, err := secretPlugin.Put(&testSecret, testSecretVal) | ||
By("Not returning an error") | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
By("Returning a non nil response") | ||
Expect(response).ShouldNot(BeNil()) | ||
}) | ||
}) | ||
When("Putting a secret to an existing secret", func() { | ||
secretPlugin, _ := secretPlugin.New() | ||
It("Should succesfully store a secret", func() { | ||
response, err := secretPlugin.Put(&testSecret, testSecretVal) | ||
By("Not returning an error") | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
By("Returning a non nil response") | ||
Expect(response).ShouldNot(BeNil()) | ||
}) | ||
}) | ||
When("Putting a secret with an empty name", func() { | ||
secretPlugin, _ := secretPlugin.New() | ||
It("Should throw an error", func() { | ||
emptySecretName := &secret.Secret{} | ||
response, err := secretPlugin.Put(emptySecretName, testSecretVal) | ||
By("Returning an error") | ||
Expect(err).Should(HaveOccurred()) | ||
|
||
By("Returning a nil response") | ||
Expect(response).Should(BeNil()) | ||
}) | ||
}) | ||
}) | ||
When("Get", func() { | ||
When("Getting a secret that exists", func() { | ||
secretPlugin, _ := secretPlugin.New() | ||
It("Should return the secret", func() { | ||
putResponse, _ := secretPlugin.Put(&testSecret, testSecretVal) | ||
response, err := secretPlugin.Access(putResponse.SecretVersion) | ||
By("Not returning an error") | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
By("Returning a response") | ||
Expect(response.SecretVersion.Secret.Name).Should(Equal(testSecret.Name)) | ||
Expect(response.Value).Should(Equal(testSecretVal)) | ||
}) | ||
}) | ||
When("Getting the latest secret", func() { | ||
secretPlugin, _ := secretPlugin.New() | ||
It("Should return the latest secret", func() { | ||
putResponse, _ := secretPlugin.Put(&testSecret, testSecretVal) | ||
response, err := secretPlugin.Access(&secret.SecretVersion{ | ||
Secret: &secret.Secret{ | ||
Name: testSecret.Name, | ||
}, | ||
Version: "latest", | ||
}) | ||
By("Not returning an error") | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
By("Returning a response") | ||
Expect(response.SecretVersion.Secret.Name).Should(Equal(testSecret.Name)) | ||
Expect(response.SecretVersion.Version).Should(Equal(putResponse.SecretVersion.Version)) | ||
Expect(response.Value).Should(Equal(testSecretVal)) | ||
}) | ||
}) | ||
When("Getting a secret that doesn't exist", func() { | ||
secretPlugin, _ := secretPlugin.New() | ||
It("Should return an error", func() { | ||
response, err := secretPlugin.Access(&secret.SecretVersion{ | ||
Secret: &secret.Secret{ | ||
Name: "test-id", | ||
}, | ||
Version: "test-version-id", | ||
}) | ||
By("Returning an error") | ||
Expect(err).Should(HaveOccurred()) | ||
By("Returning a nil response") | ||
Expect(response).Should(BeNil()) | ||
}) | ||
}) | ||
When("Getting a secret with an empty id", func() { | ||
secretPlugin, _ := secretPlugin.New() | ||
It("Should return an error", func() { | ||
response, err := secretPlugin.Access(&secret.SecretVersion{ | ||
Secret: &secret.Secret{}, | ||
Version: "test-version-id", | ||
}) | ||
By("Returning an error") | ||
Expect(err).Should(HaveOccurred()) | ||
By("Returning a nil response") | ||
Expect(response).Should(BeNil()) | ||
}) | ||
}) | ||
When("Getting a secret with an empty version id", func() { | ||
secretPlugin, _ := secretPlugin.New() | ||
It("Should return an error", func() { | ||
response, err := secretPlugin.Access(&secret.SecretVersion{ | ||
Secret: &secret.Secret{ | ||
Name: "test-id", | ||
}, | ||
}) | ||
By("Returning an error") | ||
Expect(err).Should(HaveOccurred()) | ||
By("Returning a nil response") | ||
Expect(response).Should(BeNil()) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters