Skip to content

Commit

Permalink
ENG-648 add error if sharing with user that has no keys setup (#255)
Browse files Browse the repository at this point in the history
* add error if sharing with user that has no keys setup

* fixes after code review
  • Loading branch information
zreigz authored Nov 8, 2022
1 parent 1808283 commit 12d0ae5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
33 changes: 30 additions & 3 deletions cmd/plural/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,29 @@ func TestShare(t *testing.T) {
{
name: `test "crypto share"`,
args: []string{plural.ApplicationName, "crypto", "share", "--email", "[email protected]"},
keys: []*api.PublicKey{
{
Id: "abc",
Content: "age1wqc2hk954ukemelys5gxdwlqve8ev0e88hvl3cjhfcvq65gwgvsqkmq9dn",
User: &api.User{
Email: "[email protected]",
},
},
},
},
{
name: `test "crypto share" where test user has no key setup`,
args: []string{plural.ApplicationName, "crypto", "share", "--email", "[email protected]"},
keys: []*api.PublicKey{
{
Id: "abc",
Content: "age1wqc2hk954ukemelys5gxdwlqve8ev0e88hvl3cjhfcvq65gwgvsqkmq9dn",
User: &api.User{
Email: "[email protected]",
},
},
},
expectedError: "Some of the users [[email protected]] have no keys setup",
},
}
for _, test := range tests {
Expand All @@ -126,13 +149,15 @@ func TestShare(t *testing.T) {
}(dir)
err = os.Chdir(dir)
assert.NoError(t, err)
os.Setenv("HOME", dir)
defer os.Unsetenv("HOME")
defaultConfig := pluraltest.GenDefaultConfig()
err = defaultConfig.Save(config.ConfigName)
assert.NoError(t, err)

client := mocks.NewClient(t)
if test.expectedError == "" {
client.On("ListKeys", mock.Anything).Return(nil, nil)
if test.keys != nil {
client.On("ListKeys", mock.Anything).Return(test.keys, nil)
}
app := plural.CreateNewApp(&plural.Plural{Client: client})
app.HelpName = plural.ApplicationName
Expand Down Expand Up @@ -182,7 +207,9 @@ func TestRecover(t *testing.T) {
// create temp environment
dir, err := os.MkdirTemp("", "config")
assert.NoError(t, err)
defer os.RemoveAll(dir)
defer func(path string) {
_ = os.RemoveAll(path)
}(dir)

os.Setenv("HOME", dir)
defer os.Unsetenv("HOME")
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/olekukonko/tablewriter v0.0.5
github.com/packethost/packngo v0.25.0
github.com/philopon/go-toposort v0.0.0-20170620085441-9be86dbd762f
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pluralsh/gqlclient v1.1.15
github.com/pluralsh/plural-operator v0.5.3
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,6 @@ github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZO
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 h1:Ii+DKncOVM8Cu1Hc+ETb5K+23HdAMvESYE3ZJ5b5cMI=
github.com/philopon/go-toposort v0.0.0-20170620085441-9be86dbd762f h1:WyCn68lTiytVSkk7W1K9nBiSGTSRlUOdyTnSjwrIlok=
github.com/philopon/go-toposort v0.0.0-20170620085441-9be86dbd762f/go.mod h1:/iRjX3DdSK956SzsUdV55J+wIsQ+2IBWmBrB4RvZfk4=
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4/go.mod h1:N6UoU20jOqggOuDwUaBQpluzLNDqif3kq9z2wpdYEfQ=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
Expand All @@ -937,8 +935,6 @@ github.com/pluralsh/oauth v0.9.1-0.20220520000222-d76c0e7a0db9 h1:bMkXXUksi9ym+e
github.com/pluralsh/oauth v0.9.1-0.20220520000222-d76c0e7a0db9/go.mod h1:aTUw/75rzcsbvW+/TLvWtHVDXFIdtFrDtUncOq9vHyM=
github.com/pluralsh/plural-operator v0.5.3 h1:GaPL3LgimfzKZNHt7zXzqYZpb0hgyW9noHYnkA+rqNs=
github.com/pluralsh/plural-operator v0.5.3/go.mod h1:WIXiz26/WDcUn0FA7Q1jPxmfsm98U1/JL8YpIdKVLX0=
github.com/pluralsh/polly v0.0.2 h1:KBfpxlo6ssI2Ha1Iu6hWVY8Pt3v1Po/ixwwE788QUnY=
github.com/pluralsh/polly v0.0.2/go.mod h1:GX6PeRDTRBLXNq3AgXfgJUEtfDssB7bm/JUjxDnjQ1U=
github.com/pluralsh/polly v0.0.3 h1:0wN7MXGyDKCoZepO2+ryIb5T4a01IQd0r9IGDf6bo90=
github.com/pluralsh/polly v0.0.3/go.mod h1:GX6PeRDTRBLXNq3AgXfgJUEtfDssB7bm/JUjxDnjQ1U=
github.com/pluralsh/polly v0.0.4 h1:oh89Oh5xCCfV7hZvE019KBFdbZAwwsLZtCbqYQWDppM=
Expand Down
22 changes: 22 additions & 0 deletions pkg/crypto/age.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/pluralsh/plural/pkg/utils"
"github.com/pluralsh/plural/pkg/utils/git"
"github.com/pluralsh/plural/pkg/utils/pathing"
"github.com/pluralsh/polly/algorithms"
"github.com/pluralsh/polly/containers"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/util/sets"
)
Expand Down Expand Up @@ -104,6 +106,22 @@ func Identity() (*age.X25519Identity, error) {
return generateIdentity(getAgePath())
}

func findMissingKeyForEmail(emails []string, keys []*api.PublicKey) []string {
if len(keys) == 0 || len(emails) == 0 {
// in case of empty or nil objects
return []string{}
}
emailSet := containers.ToSet[string](emails)
algorithms.Map(keys, func(key *api.PublicKey) string {
if key.User != nil && key.User.Email != "" {
emailSet.Remove(key.User.Email)
}
return ""
})

return emailSet.List()
}

func SetupAge(client api.Client, emails []string) error {
ageConfig, err := setupAgeConfig()
if err != nil {
Expand All @@ -116,6 +134,10 @@ func SetupAge(client api.Client, emails []string) error {
if err != nil {
return err
}
missingEmails := findMissingKeyForEmail(emails, keys)
if len(missingEmails) > 0 {
return fmt.Errorf("Some of the users %v have no keys setup", missingEmails)
}

present := sets.NewString()
dedupeKey := func(id *AgeIdentity) string { return fmt.Sprintf("%s::%s", id.Email, id.Key) }
Expand Down

0 comments on commit 12d0ae5

Please sign in to comment.