Skip to content

Commit

Permalink
fix: make rules cm file name configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
nsklikas committed Jan 4, 2024
1 parent 3f05710 commit 3f05b59
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func main() {
Namespace: specs.SchemasConfigMapNamespace,
}

rulesConfig := rules.NewConfig(specs.RulesConfigMapName, specs.RulesConfigMapNamespace, k8sCoreV1, oPublicClient.ApiApi())
rulesConfig := rules.NewConfig(specs.RulesConfigMapName, specs.RulesConfigFileName, specs.RulesConfigMapNamespace, k8sCoreV1, oPublicClient.ApiApi())

router := web.NewRouter(idpConfig, schemasConfig, rulesConfig, hAdminClient, kAdminClient, tracer, monitor, logger)

Expand Down
1 change: 1 addition & 0 deletions internal/config/specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ type EnvSpec struct {
SchemasConfigMapNamespace string `envconfig:"schemas_configmap_namespace" required:"true"`

RulesConfigMapName string `envconfig:"rules_configmap_name" required:"true"`
RulesConfigFileName string `envconfig:"rules_configmap_file_name" default:"admin_ui_rules.json"`
RulesConfigMapNamespace string `envconfig:"rules_configmap_namespace" required:"true"`
}
18 changes: 9 additions & 9 deletions pkg/rules/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ import (
coreV1 "k8s.io/client-go/kubernetes/typed/core/v1"
)

const (
ADMIN_UI_RULE_FILE = "admin_ui_rules.json"
)

type Config struct {
Name string
File string
Namespace string
K8s coreV1.CoreV1Interface
OkClient oathkeeper.ApiApi
}

func NewConfig(cmName, cmNamespace string, k8s coreV1.CoreV1Interface, oathkeeper oathkeeper.ApiApi) *Config {
func NewConfig(cmName, cmFile, cmNamespace string, k8s coreV1.CoreV1Interface, oathkeeper oathkeeper.ApiApi) *Config {
rulesConfig := Config{
K8s: k8s,
Name: cmName,
File: cmFile,
Namespace: cmNamespace,
OkClient: oathkeeper,
}
Expand All @@ -40,6 +38,7 @@ type Service struct {
oathkeeper oathkeeper.ApiApi

cmName string
cmFileName string
cmNamespace string

k8s coreV1.CoreV1Interface
Expand Down Expand Up @@ -114,7 +113,7 @@ func (s *Service) UpdateRule(ctx context.Context, id string, updatedRule oathkee
return err
}

cm.Data[ADMIN_UI_RULE_FILE] = rawRuleList
cm.Data[s.cmFileName] = rawRuleList

if _, err = s.k8s.ConfigMaps(s.cmNamespace).Update(ctx, cm, metaV1.UpdateOptions{}); err != nil {
return err
Expand Down Expand Up @@ -152,7 +151,7 @@ func (s *Service) CreateRule(ctx context.Context, newRule oathkeeper.Rule) error
return err
}

cm.Data[ADMIN_UI_RULE_FILE] = rawRuleList
cm.Data[s.cmFileName] = rawRuleList

if _, err = s.k8s.ConfigMaps(s.cmNamespace).Update(ctx, cm, metaV1.UpdateOptions{}); err != nil {
return err
Expand Down Expand Up @@ -188,7 +187,7 @@ func (s *Service) DeleteRule(ctx context.Context, id string) error {
return err
}

cm.Data[ADMIN_UI_RULE_FILE] = rawRuleList
cm.Data[s.cmFileName] = rawRuleList

if _, err = s.k8s.ConfigMaps(s.cmNamespace).Update(ctx, cm, metaV1.UpdateOptions{}); err != nil {
return err
Expand All @@ -204,7 +203,7 @@ func (s *Service) extractAdminRules(data map[string]string) (map[string]*oathkee
ruleMap := make(map[string]*oathkeeper.Rule)

ruleList := make([]*oathkeeper.Rule, 0)
rawRuleList, ok := data[ADMIN_UI_RULE_FILE]
rawRuleList, ok := data[s.cmFileName]
if !ok {
return ruleMap, nil
}
Expand Down Expand Up @@ -273,6 +272,7 @@ func NewService(config *Config, tracer trace.Tracer, monitor monitoring.MonitorI
s.oathkeeper = config.OkClient
s.k8s = config.K8s
s.cmName = config.Name
s.cmFileName = config.File
s.cmNamespace = config.Namespace

s.monitor = monitor
Expand Down
33 changes: 22 additions & 11 deletions pkg/rules/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestListRulesSuccess(t *testing.T) {
ctx := context.Background()
config := Config{
Name: "mock_config",
File: "admin_ui_rules.json",
Namespace: "mock_namespace",
K8s: mockCoreV1,
OkClient: mockOathkeeperApiApi,
Expand Down Expand Up @@ -102,6 +103,7 @@ func TestListRulesFails(t *testing.T) {
ctx := context.Background()
config := Config{
Name: "mock_config",
File: "admin_ui_rules.json",
Namespace: "mock_namespace",
K8s: mockCoreV1,
OkClient: mockOathkeeperApiApi,
Expand Down Expand Up @@ -163,6 +165,7 @@ func TestGetRuleSuccess(t *testing.T) {
ctx := context.Background()
config := Config{
Name: "mock_config",
File: "admin_ui_rules.json",
Namespace: "mock_namespace",
K8s: mockCoreV1,
OkClient: mockOathkeeperApiApi,
Expand Down Expand Up @@ -229,6 +232,7 @@ func TestGetRuleFails(t *testing.T) {
ctx := context.Background()
config := Config{
Name: "mock_config",
File: "admin_ui_rules.json",
Namespace: "mock_namespace",
K8s: mockCoreV1,
OkClient: mockOathkeeperApiApi,
Expand Down Expand Up @@ -289,6 +293,7 @@ func TestUpdateRuleSuccess(t *testing.T) {
ctx := context.Background()
config := Config{
Name: "mock_config",
File: "admin_ui_rules.json",
Namespace: "mock_namespace",
K8s: mockCoreV1,
OkClient: mockOathkeeperApiApi,
Expand Down Expand Up @@ -347,7 +352,7 @@ func TestUpdateRuleSuccess(t *testing.T) {
rawRuleList, _ := json.Marshal(ruleList)
cm := new(v1.ConfigMap)
cm.Data = make(map[string]string)
cm.Data[ADMIN_UI_RULE_FILE] = string(rawRuleList)
cm.Data[config.File] = string(rawRuleList)

ruleUpdate := oathkeeper.Rule{
Id: &rules2_id,
Expand Down Expand Up @@ -378,7 +383,7 @@ func TestUpdateRuleSuccess(t *testing.T) {
mockConfigMapV1.EXPECT().Get(ctx, "mock_config", gomock.Any()).Times(1).Return(cm, nil)
mockConfigMapV1.EXPECT().Update(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).DoAndReturn(
func(ctx context.Context, cm *v1.ConfigMap, opts metaV1.UpdateOptions) (*v1.ConfigMap, error) {
rules := cm.Data[ADMIN_UI_RULE_FILE]
rules := cm.Data[config.File]

if ruleIncludedInMarshalledList(ruleUpdate, rules) {
t.Fatalf("expected result to be %v not %v", string(ruleUpdatedRaw), rules)
Expand Down Expand Up @@ -410,6 +415,7 @@ func TestUpdateRuleNotFound(t *testing.T) {
ctx := context.Background()
config := Config{
Name: "mock_config",
File: "admin_ui_rules.json",
Namespace: "mock_namespace",
K8s: mockCoreV1,
OkClient: mockOathkeeperApiApi,
Expand Down Expand Up @@ -468,7 +474,7 @@ func TestUpdateRuleNotFound(t *testing.T) {
rawRuleList, _ := json.Marshal(ruleList)
cm := new(v1.ConfigMap)
cm.Data = make(map[string]string)
cm.Data[ADMIN_UI_RULE_FILE] = string(rawRuleList)
cm.Data[config.File] = string(rawRuleList)

ruleUpdate := oathkeeper.Rule{
Id: &rules3_id,
Expand Down Expand Up @@ -515,6 +521,7 @@ func TestUpdateRuleIdMismatch(t *testing.T) {
ctx := context.Background()
config := Config{
Name: "mock_config",
File: "admin_ui_rules.json",
Namespace: "mock_namespace",
K8s: mockCoreV1,
OkClient: mockOathkeeperApiApi,
Expand Down Expand Up @@ -573,7 +580,7 @@ func TestUpdateRuleIdMismatch(t *testing.T) {
rawRuleList, _ := json.Marshal(ruleList)
cm := new(v1.ConfigMap)
cm.Data = make(map[string]string)
cm.Data[ADMIN_UI_RULE_FILE] = string(rawRuleList)
cm.Data[config.File] = string(rawRuleList)

rule_update := oathkeeper.Rule{
Id: &rules3_id,
Expand Down Expand Up @@ -619,6 +626,7 @@ func TestCreateRuleSuccess(t *testing.T) {
ctx := context.Background()
config := Config{
Name: "mock_config",
File: "admin_ui_rules.json",
Namespace: "mock_namespace",
K8s: mockCoreV1,
OkClient: mockOathkeeperApiApi,
Expand Down Expand Up @@ -677,7 +685,7 @@ func TestCreateRuleSuccess(t *testing.T) {
rawRuleList, _ := json.Marshal(ruleList)
cm := new(v1.ConfigMap)
cm.Data = make(map[string]string)
cm.Data[ADMIN_UI_RULE_FILE] = string(rawRuleList)
cm.Data[config.File] = string(rawRuleList)

ruleCreate := oathkeeper.Rule{
Id: &rules3_id,
Expand Down Expand Up @@ -708,7 +716,7 @@ func TestCreateRuleSuccess(t *testing.T) {
mockConfigMapV1.EXPECT().Get(ctx, "mock_config", gomock.Any()).Times(1).Return(cm, nil)
mockConfigMapV1.EXPECT().Update(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).DoAndReturn(
func(ctx context.Context, cm *v1.ConfigMap, opts metaV1.UpdateOptions) (*v1.ConfigMap, error) {
rules := cm.Data[ADMIN_UI_RULE_FILE]
rules := cm.Data[config.File]
if ruleIncludedInMarshalledList(ruleCreate, rules) {
t.Fatalf("expected result to be %v not %v", string(ruleCreatedRaw), rules)
}
Expand Down Expand Up @@ -738,6 +746,7 @@ func TestCreateRuleAlreadyExists(t *testing.T) {
ctx := context.Background()
config := Config{
Name: "mock_config",
File: "admin_ui_rules.json",
Namespace: "mock_namespace",
K8s: mockCoreV1,
OkClient: mockOathkeeperApiApi,
Expand Down Expand Up @@ -795,7 +804,7 @@ func TestCreateRuleAlreadyExists(t *testing.T) {
rawRuleList, _ := json.Marshal(ruleList)
cm := new(v1.ConfigMap)
cm.Data = make(map[string]string)
cm.Data[ADMIN_UI_RULE_FILE] = string(rawRuleList)
cm.Data[config.File] = string(rawRuleList)

ruleCreate := oathkeeper.Rule{
Id: &rules1_id,
Expand Down Expand Up @@ -843,6 +852,7 @@ func TestDeleteRuleSuccess(t *testing.T) {
ctx := context.Background()
config := Config{
Name: "mock_config",
File: "admin_ui_rules.json",
Namespace: "mock_namespace",
K8s: mockCoreV1,
OkClient: mockOathkeeperApiApi,
Expand Down Expand Up @@ -879,16 +889,16 @@ func TestDeleteRuleSuccess(t *testing.T) {
rawRuleList, _ := json.Marshal(ruleList)
cm := new(v1.ConfigMap)
cm.Data = make(map[string]string)
cm.Data[ADMIN_UI_RULE_FILE] = string(rawRuleList)
cm.Data[config.File] = string(rawRuleList)

mockTracer.EXPECT().Start(ctx, "rules.Service.DeleteRule").Times(1).Return(ctx, trace.SpanFromContext(ctx))
mockCoreV1.EXPECT().ConfigMaps(config.Namespace).Times(2).Return(mockConfigMapV1)
mockConfigMapV1.EXPECT().Get(ctx, "mock_config", gomock.Any()).Times(1).Return(cm, nil)
mockConfigMapV1.EXPECT().Update(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).DoAndReturn(
func(ctx context.Context, cm *v1.ConfigMap, opts metaV1.UpdateOptions) (*v1.ConfigMap, error) {
rules := cm.Data[ADMIN_UI_RULE_FILE]
rules := cm.Data[config.File]
if !isMarshalledRuleListEmpty(rules) {
t.Fatalf("expected rule %s to contain empty list, not %s", ADMIN_UI_RULE_FILE, rules)
t.Fatalf("expected rule %s to contain empty list, not %s", config.File, rules)
}

return cm, nil
Expand Down Expand Up @@ -916,6 +926,7 @@ func TestDeleteRuleFailure(t *testing.T) {
ctx := context.Background()
config := Config{
Name: "mock_config",
File: "admin_ui_rules.json",
Namespace: "mock_namespace",
K8s: mockCoreV1,
OkClient: mockOathkeeperApiApi,
Expand Down Expand Up @@ -952,7 +963,7 @@ func TestDeleteRuleFailure(t *testing.T) {
rawRuleList, _ := json.Marshal(ruleList)
cm := new(v1.ConfigMap)
cm.Data = make(map[string]string)
cm.Data[ADMIN_UI_RULE_FILE] = string(rawRuleList)
cm.Data[config.File] = string(rawRuleList)

ruleForDeletion := "mocked_rule3:deny"

Expand Down

0 comments on commit 3f05b59

Please sign in to comment.