Skip to content
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 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions api/types/accessgraph/authorized_key.go
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)
}
90 changes: 90 additions & 0 deletions api/types/accessgraph/authorized_key_test.go
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()))

})
}
}
108 changes: 108 additions & 0 deletions api/types/accessgraph/private_key.go
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")
}
Comment on lines +87 to +89
Copy link
Contributor

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 has PublicKeyFingerprint 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

Copy link
Contributor Author

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.

Copy link
Contributor

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 ?


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))
}
Loading
Loading