-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,323 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
secret |
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 @@ | ||
|
||
GOPATH:=$(shell go env GOPATH) | ||
.PHONY: init | ||
init: | ||
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest | ||
go install github.com/micro/micro/v3/cmd/protoc-gen-micro@latest | ||
go install github.com/micro/micro/v3/cmd/protoc-gen-openapi@latest | ||
|
||
.PHONY: api | ||
api: | ||
protoc --openapi_out=. --proto_path=. proto/secret.proto | ||
|
||
.PHONY: proto | ||
proto: | ||
protoc --proto_path=. --micro_out=. --go_out=:. proto/secret.proto | ||
|
||
.PHONY: build | ||
build: | ||
go build -o secret *.go | ||
|
||
.PHONY: test | ||
test: | ||
go test -v ./... -cover | ||
|
||
.PHONY: docker | ||
docker: | ||
docker build . -t secret:latest |
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,8 @@ | ||
Encrypted secret storage | ||
|
||
# Secret Service | ||
|
||
Store secrets, tokens, passwords and key-value config in encrypted data storage. | ||
|
||
Keys are individually allocated spaces for values. Values are encrypted at rest. | ||
Values can be strings or JSON with path based lookup if the latter. |
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,44 @@ | ||
|
||
{ | ||
"set": [{ | ||
"title": "Set a value", | ||
"description": "Set allows you to set a value", | ||
"run_check": true, | ||
"idempotent": true, | ||
"request": { | ||
"key": "foo", | ||
"value": "bar" | ||
}, | ||
"response": {} | ||
}], | ||
"get": [{ | ||
"title": "Get a value", | ||
"description": "Get returns a value", | ||
"run_check": true, | ||
"request": { | ||
"key": "foo" | ||
}, | ||
"response": { | ||
"key": "foo", | ||
"value": "bar" | ||
} | ||
}], | ||
"delete": [{ | ||
"title": "Delete a value", | ||
"description": "Delete a value", | ||
"run_check": true, | ||
"request": { | ||
"key": "foo" | ||
}, | ||
"response": {} | ||
}], | ||
"list": [{ | ||
"title": "List all secrets", | ||
"description": "List all the stored secrets", | ||
"run_check": true, | ||
"request": {}, | ||
"response": { | ||
"keys": ["foo"] | ||
} | ||
}] | ||
} |
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,6 @@ | ||
{ | ||
"name": "secret", | ||
"icon": "🗝️", | ||
"category": "storage", | ||
"display_name": "Secrets" | ||
} |
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,3 @@ | ||
{ | ||
"key": "UTlzQWNHaFFSOEdRcHFIWVBUQk9rSnQ4TFluaUt4MmU=" | ||
} |
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,70 @@ | ||
package handler | ||
|
||
import ( | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
// encrypt/decrypt functions are taken from https://www.melvinvivas.com/how-to-encrypt-and-decrypt-data-using-aes/ | ||
|
||
func encrypt(stringToEncrypt string, key []byte) (string, error) { | ||
plaintext := []byte(stringToEncrypt) | ||
|
||
//Create a new Cipher Block from the key | ||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
//Create a new GCM - https://en.wikipedia.org/wiki/Galois/Counter_Mode | ||
//https://golang.org/pkg/crypto/cipher/#NewGCM | ||
aesGCM, err := cipher.NewGCM(block) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
//Create a nonce. Nonce should be from GCM | ||
nonce := make([]byte, aesGCM.NonceSize()) | ||
if _, err = io.ReadFull(rand.Reader, nonce); err != nil { | ||
return "", err | ||
} | ||
|
||
//Encrypt the data using aesGCM.Seal | ||
//Since we don't want to save the nonce somewhere else in this case, we add it as a prefix to the encrypted data. The first nonce argument in Seal is the prefix. | ||
ciphertext := aesGCM.Seal(nonce, nonce, plaintext, nil) | ||
return fmt.Sprintf("%x", ciphertext), nil | ||
} | ||
|
||
func decrypt(encryptedString string, key []byte) (string, error) { | ||
enc, _ := hex.DecodeString(encryptedString) | ||
|
||
//Create a new Cipher Block from the key | ||
block, err := aes.NewCipher(key) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
//Create a new GCM | ||
aesGCM, err := cipher.NewGCM(block) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
//Get the nonce size | ||
nonceSize := aesGCM.NonceSize() | ||
|
||
//Extract the nonce from the encrypted data | ||
nonce, ciphertext := enc[:nonceSize], enc[nonceSize:] | ||
|
||
//Decrypt the data | ||
plaintext, err := aesGCM.Open(nil, nonce, ciphertext, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return fmt.Sprintf("%s", plaintext), nil | ||
} |
Oops, something went wrong.