-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sec_scan][11] add
AccessGraphSettings
backend service
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
Showing
9 changed files
with
410 additions
and
13 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
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 | ||
} |
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,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())) | ||
}) | ||
} | ||
} |
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,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) | ||
} |
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
Oops, something went wrong.