-
Notifications
You must be signed in to change notification settings - Fork 3
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 #80 from tooploox/bilus/secrets-init
Add `oya secrets init` command
- Loading branch information
Showing
10 changed files
with
262 additions
and
7 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
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
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 |
---|---|---|
|
@@ -40,12 +40,15 @@ Scenario: Encrypts secrets file | |
SECRETPHRASE | ||
""" | ||
|
||
@bug | ||
Scenario: Views secrets file | ||
Given file ./secrets.oya containing | ||
""" | ||
foo: SECRETPHRASE | ||
""" | ||
Then file ./secrets.oya contains | ||
""" | ||
foo: SECRETPHRASE | ||
""" | ||
And I run "oya secrets encrypt secrets.oya" | ||
Then the command succeeds | ||
When I run "oya secrets view secrets.oya" | ||
|
@@ -85,3 +88,30 @@ Scenario: It correctly merges secrets | |
peach | ||
""" | ||
|
||
Scenario: It can quickly generate and import PGP key | ||
Given file ./Oyafile containing | ||
""" | ||
Project: Secrets | ||
all: | | ||
echo ${Oya[foo.bar]} | ||
echo ${Oya[foo.baz]} | ||
""" | ||
And file ./secrets2.oya containing | ||
""" | ||
foo: | ||
bar: banana | ||
baz: peach | ||
""" | ||
And the SOPS_PGP_FP environment variable set to "" | ||
When I run "oya secrets init --name 'Oya test key' --email '[email protected]'" | ||
And I run "oya secrets encrypt secrets2.oya" | ||
And I run "oya run all" | ||
Then the command succeeds | ||
And the command outputs | ||
""" | ||
banana | ||
peach | ||
""" | ||
And secrets2.oya is encrypted using PGP key in .sops.yaml |
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
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,81 @@ | ||
package secrets | ||
|
||
import ( | ||
"io/ioutil" | ||
"os/exec" | ||
|
||
mig "github.com/mozilla/mig/pgp" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
func generatePGPKeyPair(email, name, desc string) (KeyPair, error) { | ||
pubkey, privkey, fp, err := mig.GenerateKeyPair(name, desc, email) | ||
if err != nil { | ||
return KeyPair{}, err | ||
} | ||
return KeyPair{ | ||
Public: string(pubkey), | ||
Private: string(privkey), | ||
Fingerprint: string(fp), | ||
}, nil | ||
} | ||
|
||
type SopsYaml struct { | ||
CreationRules []CreationRule `yaml:"creation_rules"` | ||
} | ||
|
||
type CreationRule struct { | ||
PGP string `yaml:"pgp"` | ||
} | ||
|
||
func GeneratePGPSopsYaml(keyPair KeyPair) error { | ||
sops := SopsYaml{ | ||
CreationRules: []CreationRule{ | ||
{PGP: keyPair.Fingerprint}, | ||
}, | ||
} | ||
|
||
content, err := yaml.Marshal(sops) | ||
if err != nil { | ||
return err | ||
} | ||
return ioutil.WriteFile(".sops.yaml", content, 0644) | ||
} | ||
|
||
func LoadPGPSopsYaml() (SopsYaml, error) { | ||
contents, err := ioutil.ReadFile(".sops.yaml") | ||
if err != nil { | ||
return SopsYaml{}, err | ||
} | ||
|
||
var sops SopsYaml | ||
return sops, yaml.Unmarshal(contents, &sops) | ||
} | ||
|
||
func ImportPGPKeypair(keyPair KeyPair) error { | ||
cmd := exec.Command("gpg", "--import") | ||
in, err := cmd.StdinPipe() | ||
if err != nil { | ||
return err | ||
} | ||
if err = cmd.Start(); err != nil { | ||
return err | ||
} | ||
if _, err := in.Write(([]byte)(keyPair.Private)); err != nil { | ||
return err | ||
} | ||
in.Close() | ||
return cmd.Wait() | ||
} | ||
|
||
func RemovePGPKeypairs(fingerprints []string) error { | ||
for _, fingerprint := range fingerprints { | ||
if err := exec.Command("gpg", "--batch", "--yes", "--delete-secret-keys", fingerprint).Run(); err != nil { | ||
return err | ||
} | ||
if err := exec.Command("gpg", "--batch", "--yes", "--delete-key", fingerprint).Run(); err != nil { | ||
return err | ||
} | ||
} | ||
return 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