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

EVEREST-1142 | Add command for validating RBAC policy #484

Merged
merged 33 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
1e911b1
Move rbac package under /pkg
mayankshah1607 Jul 5, 2024
adf65fb
Add getModel() helper
mayankshah1607 Jul 5, 2024
a1be3c1
Add NewEnforcerFromFilepath
mayankshah1607 Jul 5, 2024
f8b97f1
Add command to validate RBAC policy
mayankshah1607 Jul 5, 2024
5fcc6cf
wire-up everything
mayankshah1607 Jul 5, 2024
0e3d9c6
WIP
mayankshah1607 Jul 5, 2024
64b8f99
Add command for validating RBAC
mayankshah1607 Jul 5, 2024
c2635c2
Add more validation + tests
mayankshah1607 Jul 8, 2024
9190e69
Print error message in output
mayankshah1607 Jul 8, 2024
a3f7793
Improvements
mayankshah1607 Jul 8, 2024
cc250b5
Fix errors
mayankshah1607 Jul 8, 2024
5ab15df
Formatting
mayankshah1607 Jul 8, 2024
4d4500c
Fix linting
mayankshah1607 Jul 8, 2024
ae32ed3
Improvements
mayankshah1607 Jul 8, 2024
3eae4a4
Run format
mayankshah1607 Jul 8, 2024
18313b2
Add package comment
mayankshah1607 Jul 8, 2024
fb0700e
uncomment
mayankshah1607 Jul 8, 2024
f8be61c
Add check to ensure non-existent resource names are not used
mayankshah1607 Jul 8, 2024
6297b7e
/namespaces should be rbac protected
mayankshah1607 Jul 8, 2024
4691fb4
Add test case for unknown resource
mayankshah1607 Jul 8, 2024
a374e02
Update swagger
mayankshah1607 Jul 8, 2024
b070c23
bug fix
mayankshah1607 Jul 8, 2024
80e8616
Fix linting
mayankshah1607 Jul 8, 2024
f5d9ac0
Merge branch 'main' into EVEREST-1142
mayankshah1607 Jul 8, 2024
230a1b5
Merge branch 'main' into EVEREST-1142
mayankshah1607 Jul 9, 2024
946a570
Merge branch 'main' into EVEREST-1142
mayankshah1607 Jul 9, 2024
1d83b97
Merge branch 'main' into EVEREST-1142
mayankshah1607 Jul 9, 2024
7b241a6
Merge branch 'main' into EVEREST-1142
mayankshah1607 Jul 10, 2024
3d6a678
fix linting
mayankshah1607 Jul 8, 2024
37da6b0
Add TestCheckResourceNames
mayankshah1607 Jul 10, 2024
89b967f
Add TestCheckRoles
mayankshah1607 Jul 10, 2024
030b951
Add TestValidateTerms
mayankshah1607 Jul 10, 2024
a604904
Merge branch 'main' into EVEREST-1142
mayankshah1607 Jul 10, 2024
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
68 changes: 34 additions & 34 deletions api/everest-server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/everest.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import (
"golang.org/x/time/rate"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/percona/everest/api/rbac"
"github.com/percona/everest/cmd/config"
"github.com/percona/everest/pkg/common"
"github.com/percona/everest/pkg/kubernetes"
"github.com/percona/everest/pkg/oidc"
"github.com/percona/everest/pkg/rbac"
"github.com/percona/everest/pkg/session"
"github.com/percona/everest/public"
)
Expand Down
2 changes: 1 addition & 1 deletion api/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/labstack/echo/v4"
"go.uber.org/zap"

"github.com/percona/everest/api/rbac"
"github.com/percona/everest/pkg/rbac"
)

// GetUserPermissions returns the permissions for the currently logged in user.
Expand Down
68 changes: 34 additions & 34 deletions client/everest-client.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion commands/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ func newSettingsCommand(l *zap.SugaredLogger) *cobra.Command {
Short: "Configure Everest settings",
}
cmd.AddCommand(settings.NewOIDCCmd(l))

cmd.AddCommand(settings.NewRBACCmd(l))
return cmd
}
20 changes: 20 additions & 0 deletions commands/settings/rbac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Package settings ...
package settings

import (
"github.com/spf13/cobra"
"go.uber.org/zap"

"github.com/percona/everest/commands/settings/rbac"
)

// NewRBACCmd returns an new RBAC sub-command.
func NewRBACCmd(l *zap.SugaredLogger) *cobra.Command {
cmd := &cobra.Command{
Use: "rbac",
Long: "Manage RBAC settings",
Short: "Manage RBAC settings",
}
cmd.AddCommand(rbac.NewValidateCommand(l))
return cmd
}
89 changes: 89 additions & 0 deletions commands/settings/rbac/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// everest
// Copyright (C) 2023 Percona LLC
//
// 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 rbac ...
package rbac

import (
"errors"
"fmt"
"net/url"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"k8s.io/client-go/tools/clientcmd"

"github.com/percona/everest/pkg/kubernetes"
"github.com/percona/everest/pkg/output"
"github.com/percona/everest/pkg/rbac"
)

// NewValidateCommand returns a new command for validating RBAC.
func NewValidateCommand(l *zap.SugaredLogger) *cobra.Command {
cmd := &cobra.Command{
Use: "validate",
Long: "Validate RBAC settings",
Short: "Validate RBAC settings",
Run: func(cmd *cobra.Command, args []string) { //nolint:revive
initValidateViperFlags(cmd)

kubeconfigPath := viper.GetString("kubeconfig")
policyFilepath := viper.GetString("policy-file")
if kubeconfigPath == "" && policyFilepath == "" {
l.Error("Either --kubeconfig or --policy-file must be set")
os.Exit(1)
}

var k *kubernetes.Kubernetes
if kubeconfigPath != "" && policyFilepath == "" {
client, err := kubernetes.New(kubeconfigPath, l)
if err != nil {
var u *url.Error
if errors.As(err, &u) || errors.Is(err, clientcmd.ErrEmptyConfig) {
l.Error("Could not connect to Kubernetes. " +
"Make sure Kubernetes is running and is accessible from this computer/server.")
}
os.Exit(1)
}
k = client
}

err := rbac.ValidatePolicy(cmd.Context(), k, policyFilepath)
if err != nil {
fmt.Fprint(os.Stdout, output.Failure("Invalid"))
msg := err.Error()
msg = strings.Join(strings.Split(msg, "\n"), " - ")
fmt.Fprintln(os.Stdout, msg)
os.Exit(1)
}
fmt.Fprintln(os.Stdout, output.Success("Valid"))
},
}
initValidateFlags(cmd)
return cmd
}

func initValidateFlags(cmd *cobra.Command) {
cmd.Flags().String("policy-file", "", "Path to the policy file to use")
}

func initValidateViperFlags(cmd *cobra.Command) {
viper.BindEnv("kubeconfig") //nolint:errcheck,gosec
viper.BindPFlag("kubeconfig", cmd.Flags().Lookup("kubeconfig")) //nolint:errcheck,gosec
viper.BindPFlag("policy-file", cmd.Flags().Lookup("policy-file")) //nolint:errcheck,gosec
}
1 change: 1 addition & 0 deletions docs/spec/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ paths:
schema:
$ref: '#/components/schemas/Error'
'/namespaces':
x-everest-resource-name: namespaces
get:
tags:
- General info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,31 @@ import (
"strings"

"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/types"

"github.com/percona/everest/pkg/kubernetes"
rbacutils "github.com/percona/everest/pkg/rbac/utils"
)

// Adapter is the ConfigMap adapter for Casbin.
// It can load policy from ConfigMap and save policy to ConfigMap.
type Adapter struct {
kubeClient *kubernetes.Kubernetes
namespacedName types.NamespacedName
l *zap.SugaredLogger
}

// NewAdapter is the constructor for Adapter.
func NewAdapter(kubeClient *kubernetes.Kubernetes, namespacedName types.NamespacedName) *Adapter {
// New constructs a new adapter that manages a policy inside a ConfigMap.
func New(
l *zap.SugaredLogger,
mayankshah1607 marked this conversation as resolved.
Show resolved Hide resolved
kubeClient *kubernetes.Kubernetes,
namespacedName types.NamespacedName,
) *Adapter {
return &Adapter{
kubeClient: kubeClient,
namespacedName: namespacedName,
l: l,
}
}

Expand Down Expand Up @@ -68,9 +75,11 @@ func (a *Adapter) LoadPolicy(model model.Model) error {
if str == "" {
continue
}
_ = persist.LoadPolicyLine(str, model)
if err := rbacutils.LoadPolicyLine(str, model); err != nil {
a.l.Error("failed to load policy", zap.Error(err))
return err
}
}

return nil
}

Expand Down
Loading
Loading