Skip to content

Commit

Permalink
[sec_scan][11] add AccessGraphSettings backend service
Browse files Browse the repository at this point in the history
This PR adds the backend service to be able to create, update and retrieve access graph configurations from Teleport backend.

This PR is part of gravitational/access-graph#637.
  • Loading branch information
tigrato committed Jul 11, 2024
1 parent a706853 commit 97c5b75
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 13 deletions.
79 changes: 79 additions & 0 deletions api/types/clusterconfig/access_graph_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
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 clusterconfig

import (
"github.com/gravitational/trace"

clusterconfigpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/clusterconfig/v1"
headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1"
"github.com/gravitational/teleport/api/types"
)

// NewAccessGraphSettings creates a new AccessGraphSettings resource.
func NewAccessGraphSettings(spec *clusterconfigpb.AccessGraphSettingsSpec) (*clusterconfigpb.AccessGraphSettings, error) {
settings := &clusterconfigpb.AccessGraphSettings{
Kind: types.KindAccessGraphSettings,
Version: types.V1,
Metadata: &headerv1.Metadata{
Name: types.MetaNameAccessGraphSettings,
},
Spec: spec,
}
if err := ValidateAccessGraphSettings(settings); err != nil {
return nil, trace.Wrap(err)
}

return settings, nil

}

// ValidateAccessGraphSettings checks that required parameters are set
func ValidateAccessGraphSettings(s *clusterconfigpb.AccessGraphSettings) error {
if s == nil {
return trace.BadParameter("AccessGraphSettings is nil")
}
if s.Metadata == nil {
return trace.BadParameter("Metadata is nil")
}
if s.Spec == nil {
return trace.BadParameter("Spec is nil")
}

if s.Metadata.Name == "" {
return trace.BadParameter("Name is unset")
}

if s.Metadata.Name != types.MetaNameAccessGraphSettings {
return trace.BadParameter("Name is not %s", types.MetaNameAccessGraphSettings)
}

if s.Kind != types.KindAccessGraphSettings {
return trace.BadParameter("Kind is not AccessGraphSettings")
}
if s.Version != types.V1 {
return trace.BadParameter("Version is not V1")
}

switch s.Spec.GetSecretsScanConfig() {
case clusterconfigpb.AccessGraphSecretsScanConfig_ACCESS_GRAPH_SECRETS_SCAN_CONFIG_ENABLED, clusterconfigpb.AccessGraphSecretsScanConfig_ACCESS_GRAPH_SECRETS_SCAN_CONFIG_DISABLED:
default:
return trace.BadParameter("SecretsScanConfig is invalid")
}

return nil
}
93 changes: 93 additions & 0 deletions api/types/clusterconfig/access_graph_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
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 clusterconfig

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"

clusterconfigpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/clusterconfig/v1"
headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1"
"github.com/gravitational/teleport/api/types"
)

func TestNewAccessGraphSettings(t *testing.T) {
tests := []struct {
name string
spec *clusterconfigpb.AccessGraphSettingsSpec
want *clusterconfigpb.AccessGraphSettings
assertErr func(*testing.T, error, ...any)
}{
{
name: "success disabled",
spec: &clusterconfigpb.AccessGraphSettingsSpec{
SecretsScanConfig: clusterconfigpb.AccessGraphSecretsScanConfig_ACCESS_GRAPH_SECRETS_SCAN_CONFIG_DISABLED,
},
assertErr: func(t *testing.T, err error, a ...any) {
require.NoError(t, err)
},
want: &clusterconfigpb.AccessGraphSettings{
Kind: types.KindAccessGraphSettings,
Version: types.V1,
Metadata: &headerv1.Metadata{
Name: types.MetaNameAccessGraphSettings,
},
Spec: &clusterconfigpb.AccessGraphSettingsSpec{
SecretsScanConfig: clusterconfigpb.AccessGraphSecretsScanConfig_ACCESS_GRAPH_SECRETS_SCAN_CONFIG_DISABLED,
},
},
},
{
name: "success enabled",
spec: &clusterconfigpb.AccessGraphSettingsSpec{
SecretsScanConfig: clusterconfigpb.AccessGraphSecretsScanConfig_ACCESS_GRAPH_SECRETS_SCAN_CONFIG_ENABLED,
},
assertErr: func(t *testing.T, err error, a ...any) {
require.NoError(t, err)
},
want: &clusterconfigpb.AccessGraphSettings{
Kind: types.KindAccessGraphSettings,
Version: types.V1,
Metadata: &headerv1.Metadata{
Name: types.MetaNameAccessGraphSettings,
},
Spec: &clusterconfigpb.AccessGraphSettingsSpec{
SecretsScanConfig: clusterconfigpb.AccessGraphSecretsScanConfig_ACCESS_GRAPH_SECRETS_SCAN_CONFIG_ENABLED,
},
},
},
{
name: "invalid",
spec: &clusterconfigpb.AccessGraphSettingsSpec{
SecretsScanConfig: 10,
},
assertErr: func(t *testing.T, err error, a ...any) {
require.ErrorContains(t, err, "SecretsScanConfig is invalid")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewAccessGraphSettings(tt.spec)
tt.assertErr(t, err)
require.Empty(t, cmp.Diff(got, tt.want, protocmp.Transform()))
})
}
}
7 changes: 7 additions & 0 deletions api/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,13 @@ const (
// KindVnetConfig is a resource which holds cluster-wide configuration for VNet.
KindVnetConfig = "vnet_config"

// KindAccessGraphSettings is a resource which holds cluster-wide configuration for dynamic access graph settings.
KindAccessGraphSettings = "access_graph_settings"

// MetaNameAccessGraphSettings is the exact name of the singleton resource holding
// access graph settings.
MetaNameAccessGraphSettings = "access-graph-settings"

// V7 is the seventh version of resources.
V7 = "v7"

Expand Down
25 changes: 25 additions & 0 deletions lib/auth/authclient/clt.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,31 @@ func (c *Client) UpsertUserNotification(ctx context.Context, notification *notif
return nil, trace.NotImplemented(notImplementedMessage)
}

// GetAccessGraphSettings gets the access graph settings from the backend.
func (c *Client) GetAccessGraphSettings(context.Context) (*clusterconfigpb.AccessGraphSettings, error) {
return nil, trace.NotImplemented(notImplementedMessage)
}

// CreateAccessGraphSettings creates the access graph settings in the backend.
func (c *Client) CreateAccessGraphSettings(context.Context, *clusterconfigpb.AccessGraphSettings) (*clusterconfigpb.AccessGraphSettings, error) {
return nil, trace.NotImplemented(notImplementedMessage)
}

// UpdateAccessGraphSettings updates the access graph settings in the backend.
func (c *Client) UpdateAccessGraphSettings(context.Context, *clusterconfigpb.AccessGraphSettings) (*clusterconfigpb.AccessGraphSettings, error) {
return nil, trace.NotImplemented(notImplementedMessage)
}

// UpsertAccessGraphSettings creates or updates the access graph settings in the backend.
func (c *Client) UpsertAccessGraphSettings(context.Context, *clusterconfigpb.AccessGraphSettings) (*clusterconfigpb.AccessGraphSettings, error) {
return nil, trace.NotImplemented(notImplementedMessage)
}

// DeleteAccessGraphSettings deletes the access graph settings from the backend.
func (c *Client) DeleteAccessGraphSettings(context.Context) error {
return trace.NotImplemented(notImplementedMessage)
}

type WebSessionReq struct {
// User is the user name associated with the session id.
User string `json:"user"`
Expand Down
37 changes: 37 additions & 0 deletions lib/services/access_graph_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package services

import (
"github.com/gravitational/trace"

clusterconfigpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/clusterconfig/v1"
)

// UnmarshalAccessGraphSettings unmarshals the AccessGraphSettings resource from JSON.
func UnmarshalAccessGraphSettings(data []byte, opts ...MarshalOption) (*clusterconfigpb.AccessGraphSettings, error) {
out, err := UnmarshalProtoResource[*clusterconfigpb.AccessGraphSettings](data, opts...)
return out, trace.Wrap(err)
}

// MarshalAccessGraphSettings marshals the AccessGraphSettings resource to JSON.
func MarshalAccessGraphSettings(c *clusterconfigpb.AccessGraphSettings, opts ...MarshalOption) ([]byte, error) {
bytes, err := MarshalProtoResource(c, opts...)
return bytes, trace.Wrap(err)
}
12 changes: 12 additions & 0 deletions lib/services/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package services
import (
"context"

clusterconfigpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/clusterconfig/v1"
"github.com/gravitational/teleport/api/types"
)

Expand Down Expand Up @@ -115,4 +116,15 @@ type ClusterConfiguration interface {
UpdateClusterMaintenanceConfig(ctx context.Context, cfg types.ClusterMaintenanceConfig) error
// DeleteClusterMaintenanceConfig deletes the maintenance config singleton.
DeleteClusterMaintenanceConfig(ctx context.Context) error

// GetAccessGraphSettings gets the access graph settings from the backend.
GetAccessGraphSettings(context.Context) (*clusterconfigpb.AccessGraphSettings, error)
// CreateAccessGraphSettings creates the access graph settings in the backend.
CreateAccessGraphSettings(context.Context, *clusterconfigpb.AccessGraphSettings) (*clusterconfigpb.AccessGraphSettings, error)
// UpdateAccessGraphSettings updates the access graph settings in the backend.
UpdateAccessGraphSettings(context.Context, *clusterconfigpb.AccessGraphSettings) (*clusterconfigpb.AccessGraphSettings, error)
// UpsertAccessGraphSettings creates or updates the access graph settings in the backend.
UpsertAccessGraphSettings(context.Context, *clusterconfigpb.AccessGraphSettings) (*clusterconfigpb.AccessGraphSettings, error)
// DeleteAccessGraphSettings deletes the access graph settings from the backend.
DeleteAccessGraphSettings(context.Context) error
}
Loading

0 comments on commit 97c5b75

Please sign in to comment.