Skip to content

Commit

Permalink
EVEREST-1470 | Allow passing YAML file to RBAC CLI commands (#676)
Browse files Browse the repository at this point in the history
Signed-off-by: Mayank Shah <mayank.shah@percona.com>
  • Loading branch information
mayankshah1607 committed Sep 17, 2024
1 parent 1bd8742 commit 8642f65
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion pkg/rbac/fileadapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package fileadapter

import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/casbin/casbin/v2/model"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"

rbacutils "github.com/percona/everest/pkg/rbac/utils"
)
Expand All @@ -17,6 +21,11 @@ type Adapter struct {
content string
}

const (
extYaml = ".yaml"
extCsv = ".csv"
)

// New returns a new adapter that reads a policy located at the given path.
func New(path string) (*Adapter, error) {
f, err := os.Open(path) //nolint:gosec
Expand All @@ -30,8 +39,27 @@ func New(path string) (*Adapter, error) {
return nil, err
}

// Retrieve the policy based on the file type.
var policy string
switch filepath.Ext(path) {
case extYaml:
cm := corev1.ConfigMap{}
if err := yaml.Unmarshal(content, &cm); err != nil {
return nil, fmt.Errorf("failed to unmarsal yaml: %w", err)
}
s, ok := cm.Data["policy.csv"]
if !ok {
return nil, errors.New("policy.csv not found in ConfigMap")
}
policy = s
case extCsv:
policy = string(content)
default:
return nil, errors.New("unsupported file format")
}

return &Adapter{
content: string(content),
content: policy,
}, nil
}

Expand Down

0 comments on commit 8642f65

Please sign in to comment.