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

Set the auto_registration.entities.repository.enabled option to true when calling repo register --all #3876

Merged
merged 1 commit into from
Jul 12, 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
86 changes: 4 additions & 82 deletions cmd/cli/app/provider/provider_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package provider

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
Expand All @@ -28,7 +27,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/structpb"

"github.com/stacklok/minder/internal/util/cli"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
Expand Down Expand Up @@ -108,14 +106,6 @@ var (
}
)

type configStruct struct {
*minderv1.ProviderConfig
//nolint:lll
GitHub *minderv1.GitHubProviderConfig `json:"github,omitempty" yaml:"github" mapstructure:"github" validate:"required"`
//nolint:lll
GitHubApp *minderv1.GitHubAppProviderConfig `json:"github_app,omitempty" yaml:"github_app" mapstructure:"github_app" validate:"required"`
}

// UpdateProviderCommand is the command for enrolling a provider
//
//nolint:gocyclo
Expand All @@ -141,42 +131,10 @@ func UpdateProviderCommand(
unsetAttrs := viper.GetStringSlice("unset-attribute")

client := minderv1.NewProvidersServiceClient(conn)
resp, err := client.GetProvider(ctx, &minderv1.GetProviderRequest{
Context: &minderv1.Context{
Project: &project,
},
Name: providerName,
})
if err != nil {
return cli.MessageAndError("Failed to get provider", err)
}
if resp.GetProvider() == nil {
return cli.MessageAndError(
"could not retrieve provider",
errors.New("provider was empty"),
)
}

provider := resp.GetProvider()
bytes, err := provider.GetConfig().MarshalJSON()
serde, err := cli.GetProviderConfig(ctx, client, project, providerName)
if err != nil {
// TODO this is likely to be an internal error and
// should be mapped to a more suitable user-facing
// error.
return cli.MessageAndError(
"invalid config",
fmt.Errorf("error marshalling provider config: %w", err),
)
}
serde := &configStruct{}
if err := json.Unmarshal(bytes, &serde); err != nil {
// TODO this is likely to be an internal error and
// should be mapped to a more suitable user-facing
// error.
return cli.MessageAndError(
"invalid config",
fmt.Errorf("error unmarshalling provider config: %w", err),
)
return cli.MessageAndError("failed to get provider config", err)
}

config := serde.ProviderConfig
Expand Down Expand Up @@ -230,49 +188,13 @@ func UpdateProviderCommand(
}

serde.ProviderConfig = config
var structConfig map[string]any
bytes, err = json.Marshal(serde)
if err != nil {
// TODO this is likely to be an internal error and
// should be mapped to a more suitable user-facing
// error.
return cli.MessageAndError(
"invalid config",
err,
)
}
if err := json.Unmarshal(bytes, &structConfig); err != nil {
// TODO this is likely to be an internal error and
// should be mapped to a more suitable user-facing
// error.
return cli.MessageAndError(
"invalid configuration",
err,
)
}

cfg, err := structpb.NewStruct(structConfig)
err = cli.SetProviderConfig(ctx, client, project, providerName, serde)
if err != nil {
return cli.MessageAndError("invalid config patch", err)
}

req := &minderv1.PatchProviderRequest{
Context: &minderv1.Context{
Project: &project,
Provider: &providerName,
},
Patch: &minderv1.Provider{
Config: cfg,
},
}

_, err = client.PatchProvider(ctx, req)
if err != nil {
return cli.MessageAndError("failed calling minder", err)
return cli.MessageAndError("failed to update provider", err)
}

cmd.Println("Provider updated successfully")

return nil
}

Expand Down
55 changes: 54 additions & 1 deletion cmd/cli/app/repo/repo_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"

"github.com/stacklok/minder/internal/util/cli"
"github.com/stacklok/minder/internal/util/cli/table"
Expand Down Expand Up @@ -58,7 +59,14 @@ func RegisterCmd(ctx context.Context, cmd *cobra.Command, _ []string, conn *grpc
}

providerClient := minderv1.NewProvidersServiceClient(conn)
_, err := providerClient.ReconcileEntityRegistration(ctx, &minderv1.ReconcileEntityRegistrationRequest{

err := enableAutoRegistration(ctx, providerClient, project, provider)
if err != nil {
return cli.MessageAndError("Error enabling auto registration", err)
}
cmd.Println("Enabled auto registration for future repositories")

_, err = providerClient.ReconcileEntityRegistration(ctx, &minderv1.ReconcileEntityRegistrationRequest{
Context: &minderv1.Context{
Provider: &provider,
Project: &project,
Expand Down Expand Up @@ -258,6 +266,51 @@ func printRepoRegistrationStatus(cmd *cobra.Command, results []*minderv1.Registe
t.Render()
}

func enableAutoRegistration(
ctx context.Context, provCli minderv1.ProvidersServiceClient,
project, providerName string,
) error {
serde, err := cli.GetProviderConfig(ctx, provCli, project, providerName)
if err != nil {
return cli.MessageAndError("failed to get provider config", err)
}

if serde == nil {
serde = &cli.ProviderConfigUnion{}
}

if serde.ProviderConfig == nil {
serde.ProviderConfig = &minderv1.ProviderConfig{}
}

if serde.AutoRegistration == nil {
serde.AutoRegistration = &minderv1.AutoRegistration{}
}

if serde.AutoRegistration.Entities == nil {
serde.AutoRegistration.Entities = make(map[string]*minderv1.EntityAutoRegistrationConfig)
}

repoReg, ok := serde.AutoRegistration.Entities[minderv1.Entity_ENTITY_REPOSITORIES.ToString()]
if !ok {
repoReg = &minderv1.EntityAutoRegistrationConfig{}
}

if repoReg.GetEnabled() {
return cli.MessageAndError("auto registration is already enabled", nil)
}

repoReg.Enabled = proto.Bool(true)
serde.AutoRegistration.Entities[minderv1.Entity_ENTITY_REPOSITORIES.ToString()] = repoReg

err = cli.SetProviderConfig(ctx, provCli, project, providerName, serde)
if err != nil {
return cli.MessageAndError("failed to update provider", err)
}

return nil
}

func printWarnings(cmd *cobra.Command, warnings []string) {
for _, warning := range warnings {
cmd.Println(warning)
Expand Down
122 changes: 122 additions & 0 deletions internal/util/cli/providerconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//
// Copyright 2024 Stacklok, 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 cli

import (
"context"
"encoding/json"
"fmt"

"google.golang.org/protobuf/types/known/structpb"

minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)

// ProviderConfigUnion is a union type for the different provider configurations
// this is a temporary kludge until we can autogenerate the possible attributes
type ProviderConfigUnion struct {
*minderv1.ProviderConfig
//nolint:lll
GitHub *minderv1.GitHubProviderConfig `json:"github,omitempty" yaml:"github" mapstructure:"github" validate:"required"`
//nolint:lll
GitHubApp *minderv1.GitHubAppProviderConfig `json:"github_app,omitempty" yaml:"github_app" mapstructure:"github_app" validate:"required"`
}

// GetProviderConfig retrieves the provider configuration from the minder service
func GetProviderConfig(
ctx context.Context,
provCli minderv1.ProvidersServiceClient,
project, providerName string,
) (*ProviderConfigUnion, error) {
resp, err := provCli.GetProvider(ctx, &minderv1.GetProviderRequest{
Context: &minderv1.Context{
Project: &project,
},
Name: providerName,
})

if err != nil {
return nil, fmt.Errorf("failed to get provider: %w", err)
}
if resp.GetProvider() == nil {
return nil, fmt.Errorf("could not retrieve provider, provider was empty")
}

provider := resp.GetProvider()
bytes, err := provider.GetConfig().MarshalJSON()
if err != nil {
// TODO this is likely to be an internal error and
// should be mapped to a more suitable user-facing
// error.
return nil, fmt.Errorf("error marshalling provider config: %w", err)
}

serde := &ProviderConfigUnion{}
if err := json.Unmarshal(bytes, &serde); err != nil {
// TODO this is likely to be an internal error and
// should be mapped to a more suitable user-facing
// error.
return nil, fmt.Errorf("error unmarshalling provider config: %w", err)
}

return serde, nil
}

// SetProviderConfig sets the provider configuration in the minder service
func SetProviderConfig(
ctx context.Context,
provCli minderv1.ProvidersServiceClient,
project, providerName string,
serde *ProviderConfigUnion,
) error {
var structConfig map[string]any

bytes, err := json.Marshal(serde)
if err != nil {
// TODO this is likely to be an internal error and
// should be mapped to a more suitable user-facing
// error.
return fmt.Errorf("invalid config")
}
if err := json.Unmarshal(bytes, &structConfig); err != nil {
// TODO this is likely to be an internal error and
// should be mapped to a more suitable user-facing
// error.
return fmt.Errorf("invalid configuration")
}

cfg, err := structpb.NewStruct(structConfig)
if err != nil {
return fmt.Errorf("invalid config patch: %w", err)
}

req := &minderv1.PatchProviderRequest{
Context: &minderv1.Context{
Project: &project,
Provider: &providerName,
},
Patch: &minderv1.Provider{
Config: cfg,
},
}

_, err = provCli.PatchProvider(ctx, req)
if err != nil {
return fmt.Errorf("failed calling minder: %w", err)
}

return nil
}