Skip to content

Commit

Permalink
Merge pull request #78 from /issues/56
Browse files Browse the repository at this point in the history
Implement gRPC provision plugins
  • Loading branch information
markgoddard authored Dec 17, 2024
2 parents b246438 + 38e171f commit 5ac82fb
Show file tree
Hide file tree
Showing 36 changed files with 954 additions and 418 deletions.
6 changes: 3 additions & 3 deletions cmd/cofidectl/cmd/apbinding/apbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c *APBindingCommand) GetListCommand() *cobra.Command {
Long: apBindingListCmdDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down Expand Up @@ -149,7 +149,7 @@ func (c *APBindingCommand) GetAddCommand() *cobra.Command {
Long: apBindingAddCmdDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down Expand Up @@ -191,7 +191,7 @@ func (c *APBindingCommand) GetDelCommand() *cobra.Command {
Long: apBindingDelCmdDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/cofidectl/cmd/attestationpolicy/attestationpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (c *AttestationPolicyCommand) GetListCommand() *cobra.Command {
Long: attestationPolicyListCmdDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (c *AttestationPolicyCommand) GetAddK8sCommand() *cobra.Command {
Long: attestationPolicyAddK8sCmdDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/cofidectl/cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ func (d *DownCommand) DownCmd() *cobra.Command {
Long: downCmdDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := d.cmdCtx.PluginManager.GetDataSource()
ds, err := d.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}

provision, err := d.cmdCtx.PluginManager.GetProvision(cmd.Context())
if err != nil {
return err
}

provision := d.cmdCtx.PluginManager.GetProvision()
statusCh, err := provision.TearDown(cmd.Context(), ds)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions cmd/cofidectl/cmd/federation/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
trust_zone_proto "github.com/cofide/cofide-api-sdk/gen/go/proto/trust_zone/v1alpha1"
cmdcontext "github.com/cofide/cofidectl/pkg/cmd/context"

"github.com/cofide/cofidectl/pkg/spire"
kubeutil "github.com/cofide/cofidectl/pkg/kube"
"github.com/cofide/cofidectl/pkg/provider/helm"
"github.com/cofide/cofidectl/pkg/spire"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -57,7 +57,7 @@ func (c *FederationCommand) GetListCommand() *cobra.Command {
Long: federationListCmdDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down Expand Up @@ -181,7 +181,7 @@ func (c *FederationCommand) GetAddCommand() *cobra.Command {
Long: federationAddCmdDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down
12 changes: 4 additions & 8 deletions cmd/cofidectl/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,17 @@ func (i *InitCommand) GetRootCommand() *cobra.Command {
Long: initRootCmdDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
var pluginName string
plugins := manager.GetDefaultPlugins()
if opts.enableConnect {
if ok, _ := plugin.PluginExists(connectPluginName); ok {
pluginName = connectPluginName
fmt.Println(`Please run "cofidectl connect init"`)
} else {
fmt.Println("👀 get in touch with us at [email protected] to find out more")
os.Exit(1)
}
} else {
// Default to the local file data source.
pluginName = manager.LocalPluginName
os.Exit(1)
}

_, err := i.cmdCtx.PluginManager.Init(pluginName, nil)
return err
return i.cmdCtx.PluginManager.Init(cmd.Context(), plugins, nil)
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/cofidectl/cmd/trustzone/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *HelmCommand) GetOverrideCommand() *cobra.Command {
Long: helmOverrideCmdDesc,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func (c *HelmCommand) GetValuesCommand() *cobra.Command {
Long: helmValuesCmdDesc,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/cofidectl/cmd/trustzone/trustzone.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *TrustZoneCommand) GetListCommand() *cobra.Command {
Long: trustZoneListCmdDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func (c *TrustZoneCommand) GetAddCommand() *cobra.Command {
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down Expand Up @@ -193,7 +193,7 @@ func (c *TrustZoneCommand) GetStatusCommand() *cobra.Command {
Long: trustZoneStatusCmdDesc,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := c.cmdCtx.PluginManager.GetDataSource()
ds, err := c.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/cofidectl/cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ func (u *UpCommand) UpCmd() *cobra.Command {
Long: upCmdDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ds, err := u.cmdCtx.PluginManager.GetDataSource()
ds, err := u.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}

provision, err := u.cmdCtx.PluginManager.GetProvision(cmd.Context())
if err != nil {
return err
}

provision := u.cmdCtx.PluginManager.GetProvision()
statusCh, err := provision.Deploy(cmd.Context(), ds, kubeCfgFile)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/cofidectl/cmd/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (w *WorkloadCommand) GetListCommand() *cobra.Command {
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
ds, err := w.cmdCtx.PluginManager.GetDataSource()
ds, err := w.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (w *WorkloadCommand) GetDiscoverCommand() *cobra.Command {
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
ds, err := w.cmdCtx.PluginManager.GetDataSource()
ds, err := w.cmdCtx.PluginManager.GetDataSource(cmd.Context())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.23.4
require (
buf.build/go/protoyaml v0.3.0
cuelang.org/go v0.10.1
github.com/cofide/cofide-api-sdk v0.3.1-0.20241211140859-1388b27cdec8
github.com/cofide/cofide-api-sdk v0.4.1-0.20241212134830-527e2164f012
github.com/fatih/color v1.18.0
github.com/gofrs/flock v0.12.1
github.com/google/go-cmp v0.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cockroachdb/apd/v3 v3.2.1 h1:U+8j7t0axsIgvQUqthuNm82HIrYXodOV2iWLWtEaIwg=
github.com/cockroachdb/apd/v3 v3.2.1/go.mod h1:klXJcjp+FffLTHlhIG69tezTDvdP065naDsHzKhYSqc=
github.com/cofide/cofide-api-sdk v0.3.1-0.20241211140859-1388b27cdec8 h1:WArYCwwLLDrfxz6HUDY+vuhTPt8plcde4rvLoZVSKyU=
github.com/cofide/cofide-api-sdk v0.3.1-0.20241211140859-1388b27cdec8/go.mod h1:u2iATR4IbZm9ruIBN734UjVuO3XQKPAFViIY3Xr6kTA=
github.com/cofide/cofide-api-sdk v0.4.1-0.20241212134830-527e2164f012 h1:XJe+gZeK8YFYULInSQ10+BApaO872hPxZSp7ofJjyoU=
github.com/cofide/cofide-api-sdk v0.4.1-0.20241212134830-527e2164f012/go.mod h1:u2iATR4IbZm9ruIBN734UjVuO3XQKPAFViIY3Xr6kTA=
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
github.com/containerd/containerd v1.7.23 h1:H2CClyUkmpKAGlhQp95g2WXHfLYc7whAuvZGBNYOOwQ=
Expand Down
13 changes: 9 additions & 4 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,47 @@ import (
"buf.build/go/protoyaml"
attestation_policy_proto "github.com/cofide/cofide-api-sdk/gen/go/proto/attestation_policy/v1alpha1"
config_proto "github.com/cofide/cofide-api-sdk/gen/go/proto/config/v1alpha1"
pluginspb "github.com/cofide/cofide-api-sdk/gen/go/proto/plugins/v1alpha1"
trust_zone_proto "github.com/cofide/cofide-api-sdk/gen/go/proto/trust_zone/v1alpha1"
"google.golang.org/protobuf/types/known/structpb"
)

// Config describes the cofide.yaml configuration file format.
type Config struct {
DataSource string
TrustZones []*trust_zone_proto.TrustZone
AttestationPolicies []*attestation_policy_proto.AttestationPolicy
PluginConfig map[string]*structpb.Struct
Plugins *pluginspb.Plugins
}

func NewConfig() *Config {
return &Config{
DataSource: "",
TrustZones: []*trust_zone_proto.TrustZone{},
AttestationPolicies: []*attestation_policy_proto.AttestationPolicy{},
PluginConfig: map[string]*structpb.Struct{},
Plugins: &pluginspb.Plugins{},
}
}

func newConfigFromProto(proto *config_proto.Config) *Config {
plugins := proto.GetPlugins()
if plugins == nil {
plugins = &pluginspb.Plugins{}
}
return &Config{
DataSource: proto.GetDataSource(),
TrustZones: proto.TrustZones,
AttestationPolicies: proto.AttestationPolicies,
PluginConfig: proto.PluginConfig,
Plugins: plugins,
}
}

func (c *Config) toProto() *config_proto.Config {
return &config_proto.Config{
DataSource: &c.DataSource,
TrustZones: c.TrustZones,
AttestationPolicies: c.AttestationPolicies,
PluginConfig: c.PluginConfig,
Plugins: c.Plugins,
}
}

Expand Down
36 changes: 12 additions & 24 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"testing"

attestation_policy_proto "github.com/cofide/cofide-api-sdk/gen/go/proto/attestation_policy/v1alpha1"
pluginspb "github.com/cofide/cofide-api-sdk/gen/go/proto/plugins/v1alpha1"
trust_zone_proto "github.com/cofide/cofide-api-sdk/gen/go/proto/trust_zone/v1alpha1"
"github.com/cofide/cofidectl/internal/pkg/test/fixtures"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/testing/protocmp"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/structpb"
)

Expand All @@ -25,14 +25,13 @@ func TestConfig_YAMLMarshall(t *testing.T) {
{
name: "default",
config: &Config{
DataSource: "local",
Plugins: &pluginspb.Plugins{},
},
wantFile: "default.yaml",
},
{
name: "full",
config: &Config{
DataSource: "fake-plugin",
TrustZones: []*trust_zone_proto.TrustZone{
fixtures.TrustZone("tz1"),
fixtures.TrustZone("tz2"),
Expand All @@ -46,6 +45,7 @@ func TestConfig_YAMLMarshall(t *testing.T) {
"plugin1": fixtures.PluginConfig("plugin1"),
"plugin2": fixtures.PluginConfig("plugin2"),
},
Plugins: fixtures.Plugins("plugins1"),
},
wantFile: "full.yaml",
},
Expand Down Expand Up @@ -73,17 +73,16 @@ func TestConfig_YAMLUnmarshall(t *testing.T) {
name: "default",
file: "default.yaml",
want: &Config{
DataSource: "local",
TrustZones: []*trust_zone_proto.TrustZone{},
AttestationPolicies: []*attestation_policy_proto.AttestationPolicy{},
PluginConfig: map[string]*structpb.Struct{},
Plugins: &pluginspb.Plugins{},
},
},
{
name: "full",
file: "full.yaml",
want: &Config{
DataSource: "fake-plugin",
TrustZones: []*trust_zone_proto.TrustZone{
fixtures.TrustZone("tz1"),
fixtures.TrustZone("tz2"),
Expand All @@ -97,19 +96,16 @@ func TestConfig_YAMLUnmarshall(t *testing.T) {
"plugin1": fixtures.PluginConfig("plugin1"),
"plugin2": fixtures.PluginConfig("plugin2"),
},
Plugins: fixtures.Plugins("plugins1"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
yamlConfig := readTestConfig(t, tt.file)
got, err := unmarshalYAML(yamlConfig)
if err != nil {
t.Fatalf("error unmarshalling configuration from YAML: %v", err)
}
if diff := cmp.Diff(got, tt.want, protocmp.Transform()); diff != "" {
t.Errorf("yaml.Unmarshall() mismatch (-want,+got):\n%s", diff)
}
require.NoError(t, err, err)
assert.EqualExportedValues(t, tt.want, got)
})
}
}
Expand Down Expand Up @@ -153,12 +149,8 @@ func TestConfig_GetTrustZoneByName(t *testing.T) {
TrustZones: tt.trustZones,
}
gotTz, gotOk := c.GetTrustZoneByName(tt.trustZone)
if diff := cmp.Diff(tt.wantTz, gotTz, protocmp.Transform()); diff != "" {
t.Errorf("Config.GetTrustZoneByName() mismatch (-want,+got):\n%s", diff)
}
if gotOk != tt.wantOk {
t.Errorf("Config.GetTrustZoneByName() got1 = %v, want %v", gotOk, tt.wantOk)
}
assert.EqualExportedValues(t, tt.wantTz, gotTz)
assert.Equal(t, tt.wantOk, gotOk)
})
}
}
Expand Down Expand Up @@ -202,12 +194,8 @@ func TestConfig_GetAttestationPolicyByName(t *testing.T) {
AttestationPolicies: tt.policies,
}
gotAp, gotOk := c.GetAttestationPolicyByName(tt.policy)
if diff := cmp.Diff(tt.wantAp, gotAp, protocmp.Transform()); diff != "" {
t.Errorf("Config.GetAttestationPolicyByName() mismatch (-want,+got):\n%s", diff)
}
if gotOk != tt.wantOk {
t.Errorf("Config.GetAttestationPolicyByName() got1 = %v, want %v", gotOk, tt.wantOk)
}
assert.EqualExportedValues(t, tt.wantAp, gotAp)
assert.Equal(t, tt.wantOk, gotOk)
})
}
}
Loading

0 comments on commit 5ac82fb

Please sign in to comment.