-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[sec_scan][5] add secrets backend service #43543
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,90 @@ | ||
/* | ||
Copyright 2024 Gravitational, Inc. | ||
|
||
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 accessgraph | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/gravitational/trace" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
|
||
accessgraphv1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/accessgraph/v1" | ||
headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1" | ||
"github.com/gravitational/teleport/api/types" | ||
) | ||
|
||
const ( | ||
authorizedKeyDefaultKeyTTL = 8 * time.Hour | ||
) | ||
|
||
// NewAuthorizedKey creates a new SSH authorized key resource. | ||
func NewAuthorizedKey(spec *accessgraphv1pb.AuthorizedKeySpec) (*accessgraphv1pb.AuthorizedKey, error) { | ||
name := authKeyHashNameKey(spec) | ||
authKey := &accessgraphv1pb.AuthorizedKey{ | ||
Kind: types.KindAccessGraphSecretAuthorizedKey, | ||
Version: types.V1, | ||
Metadata: &headerv1.Metadata{ | ||
Name: name, | ||
Expires: timestamppb.New( | ||
time.Now().Add(authorizedKeyDefaultKeyTTL), | ||
), | ||
}, | ||
Spec: spec, | ||
} | ||
if err := ValidateAuthorizedKey(authKey); err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
|
||
return authKey, nil | ||
} | ||
|
||
// ValidateAuthorizedKey checks that required parameters are set | ||
// for the specified AuthorizedKey | ||
func ValidateAuthorizedKey(k *accessgraphv1pb.AuthorizedKey) error { | ||
if k == nil { | ||
return trace.BadParameter("AuthorizedKey is nil") | ||
} | ||
if k.Metadata == nil { | ||
return trace.BadParameter("Metadata is nil") | ||
} | ||
if k.Spec == nil { | ||
return trace.BadParameter("Spec is nil") | ||
} | ||
|
||
if k.Spec.HostId == "" { | ||
return trace.BadParameter("HostId is unset") | ||
} | ||
if k.Spec.HostUser == "" { | ||
return trace.BadParameter("HostUser is unset") | ||
} | ||
if k.Spec.KeyFingerprint == "" { | ||
return trace.BadParameter("KeyFingerprint is unset") | ||
} | ||
|
||
if k.Metadata.Name == "" { | ||
return trace.BadParameter("Name is unset") | ||
} | ||
if k.Metadata.Name != authKeyHashNameKey(k.Spec) { | ||
return trace.BadParameter("Name must be derived from the key fields") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func authKeyHashNameKey(k *accessgraphv1pb.AuthorizedKeySpec) string { | ||
return hashComp(k.HostId, k.HostUser, k.KeyFingerprint) | ||
} |
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,90 @@ | ||
/* | ||
Copyright 2024 Gravitational, Inc. | ||
|
||
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 accessgraph | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/testing/protocmp" | ||
|
||
accessgraphv1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/accessgraph/v1" | ||
) | ||
|
||
func TestAuthorizedKey(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
spec *accessgraphv1pb.AuthorizedKeySpec | ||
errValidation require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "valid", | ||
spec: &accessgraphv1pb.AuthorizedKeySpec{ | ||
HostId: uuid.New().String(), | ||
KeyFingerprint: "fingerprint", | ||
HostUser: "user", | ||
}, | ||
errValidation: require.NoError, | ||
}, | ||
{ | ||
name: "missing fingerprint", | ||
spec: &accessgraphv1pb.AuthorizedKeySpec{ | ||
HostId: uuid.New().String(), | ||
KeyFingerprint: "", | ||
HostUser: "user", | ||
}, | ||
errValidation: func(t require.TestingT, err error, i ...any) { | ||
require.ErrorContains(t, err, "KeyFingerprint is unset") | ||
}, | ||
}, | ||
{ | ||
name: "missing user", | ||
spec: &accessgraphv1pb.AuthorizedKeySpec{ | ||
HostId: uuid.New().String(), | ||
KeyFingerprint: "fingerprint", | ||
HostUser: "", | ||
}, | ||
errValidation: func(t require.TestingT, err error, i ...any) { | ||
require.ErrorContains(t, err, "HostUser is unset") | ||
}, | ||
}, | ||
{ | ||
name: "missing HostID", | ||
spec: &accessgraphv1pb.AuthorizedKeySpec{ | ||
KeyFingerprint: "fingerprint", | ||
HostUser: "user", | ||
}, | ||
errValidation: func(t require.TestingT, err error, i ...any) { | ||
require.ErrorContains(t, err, "HostId is unset") | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
privKey, err := NewAuthorizedKey(tt.spec) | ||
tt.errValidation(t, err) | ||
if err != nil { | ||
return | ||
} | ||
require.NotEmpty(t, privKey.Metadata.Name) | ||
require.Empty(t, cmp.Diff(tt.spec, privKey.Spec, protocmp.Transform())) | ||
|
||
}) | ||
} | ||
} |
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,108 @@ | ||
/* | ||
Copyright 2024 Gravitational, Inc. | ||
|
||
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 accessgraph | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
|
||
"github.com/gravitational/trace" | ||
|
||
accessgraphv1pb "github.com/gravitational/teleport/api/gen/proto/go/teleport/accessgraph/v1" | ||
headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1" | ||
"github.com/gravitational/teleport/api/types" | ||
) | ||
|
||
// NewPrivateKey creates a new SSH Private key resource with a generated name based on the spec. | ||
func NewPrivateKey(spec *accessgraphv1pb.PrivateKeySpec) (*accessgraphv1pb.PrivateKey, error) { | ||
name := privKeyHashNameKey(spec) | ||
v, err := NewPrivateKeyWithName(name, spec) | ||
|
||
return v, trace.Wrap(err) | ||
} | ||
|
||
// NewPrivateKeyWithName creates a new SSH Private key resource. | ||
func NewPrivateKeyWithName(name string, spec *accessgraphv1pb.PrivateKeySpec) (*accessgraphv1pb.PrivateKey, error) { | ||
privKey := &accessgraphv1pb.PrivateKey{ | ||
Kind: types.KindAccessGraphSecretPrivateKey, | ||
Version: types.V1, | ||
Metadata: &headerv1.Metadata{ | ||
Name: name, | ||
}, | ||
Spec: spec, | ||
} | ||
if err := ValidatePrivateKey(privKey); err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
|
||
return privKey, nil | ||
} | ||
|
||
// ValidatePrivateKey checks that required parameters are set | ||
// for the specified PrivateKey | ||
func ValidatePrivateKey(k *accessgraphv1pb.PrivateKey) error { | ||
if k == nil { | ||
return trace.BadParameter("PrivateKey is nil") | ||
} | ||
if k.Metadata == nil { | ||
return trace.BadParameter("Metadata is nil") | ||
} | ||
if k.Spec == nil { | ||
return trace.BadParameter("Spec is nil") | ||
} | ||
|
||
if k.Kind != types.KindAccessGraphSecretPrivateKey { | ||
return trace.BadParameter("Kind is invalid") | ||
} | ||
|
||
if k.Version != types.V1 { | ||
return trace.BadParameter("Version is invalid") | ||
} | ||
|
||
switch k.Spec.PublicKeyMode { | ||
case accessgraphv1pb.PublicKeyMode_PUBLIC_KEY_MODE_PROTECTED, | ||
accessgraphv1pb.PublicKeyMode_PUBLIC_KEY_MODE_PUB_FILE, | ||
accessgraphv1pb.PublicKeyMode_PUBLIC_KEY_MODE_DERIVED: | ||
default: | ||
return trace.BadParameter("PublicKeyMode is invalid") | ||
} | ||
|
||
if k.Spec.DeviceId == "" { | ||
return trace.BadParameter("DeviceId is unset") | ||
} | ||
if k.Spec.PublicKeyFingerprint == "" && k.Spec.PublicKeyMode != accessgraphv1pb.PublicKeyMode_PUBLIC_KEY_MODE_PROTECTED { | ||
return trace.BadParameter("PublicKeyFingerprint is unset") | ||
} | ||
|
||
if k.Metadata.Name == "" { | ||
return trace.BadParameter("Name is unset") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func privKeyHashNameKey(k *accessgraphv1pb.PrivateKeySpec) string { | ||
return hashComp(k.DeviceId, k.PublicKeyFingerprint) | ||
} | ||
|
||
func hashComp(values ...string) string { | ||
h := sha256.New() | ||
for _, value := range values { | ||
h.Write([]byte(value)) | ||
} | ||
return hex.EncodeToString(h.Sum(nil)) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit confusing the there
PrivateKeySpec
hasPublicKeyFingerprint
and itself the references to Public Key and the any uniq members related to private key.Does it make sense to rename it to something like: SSHKeyDetails
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the PrivateKeySpec holds the public key fingerprint.
We do read the private key, extract the public key from it and compute the SHA256 fingerprint before sending it to teleport.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the private key needs to be read at first place. Does it make sense to read the public key and get the signature without read to read sensitive private key ?