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

Multi tenant #8

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix lint
  • Loading branch information
florindragos committed Jul 11, 2024
commit 011e7a89d14caf3552066b4a2a292a307774180f
4 changes: 2 additions & 2 deletions pkg/app/handlers/groups/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func (u GroupResourceHandler) Create(r *http.Request, attributes scim.ResourceAt
u.logger.Error().Err(err).Msg("failed to get directory client")
return scim.Resource{}, serrors.ScimErrorInternal
}
scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context())
scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context(), u.cfg.SCIM.SCIMConfigKey)
if err != nil {
return scim.Resource{}, err
}
scimConfig, err := convert.TransformConfigFromMap(u.cfg.SCIM.TransformDefaults, scimConfigMap)
scimConfig, err := convert.TransformConfigFromMap(&u.cfg.SCIM.TransformDefaults, scimConfigMap)
if err != nil {
return scim.Resource{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/handlers/groups/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ func (u GroupResourceHandler) Delete(r *http.Request, id string) error {
return serrors.ScimErrorInternal
}

scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context())
scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context(), u.cfg.SCIM.SCIMConfigKey)
if err != nil {
return err
}
scimConfig, err := convert.TransformConfigFromMap(u.cfg.SCIM.TransformDefaults, scimConfigMap)
scimConfig, err := convert.TransformConfigFromMap(&u.cfg.SCIM.TransformDefaults, scimConfigMap)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/handlers/groups/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func (u GroupResourceHandler) Get(r *http.Request, id string) (scim.Resource, er
return scim.Resource{}, serrors.ScimErrorInternal
}

scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context())
scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context(), u.cfg.SCIM.SCIMConfigKey)
if err != nil {
return scim.Resource{}, err
}
scimConfig, err := convert.TransformConfigFromMap(u.cfg.SCIM.TransformDefaults, scimConfigMap)
scimConfig, err := convert.TransformConfigFromMap(&u.cfg.SCIM.TransformDefaults, scimConfigMap)
if err != nil {
return scim.Resource{}, err
}
Expand Down Expand Up @@ -59,11 +59,11 @@ func (u GroupResourceHandler) GetAll(r *http.Request, params scim.ListRequestPar
return scim.Page{}, serrors.ScimErrorInternal
}

scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context())
scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context(), u.cfg.SCIM.SCIMConfigKey)
if err != nil {
return scim.Page{}, err
}
scimConfig, err := convert.TransformConfigFromMap(u.cfg.SCIM.TransformDefaults, scimConfigMap)
scimConfig, err := convert.TransformConfigFromMap(&u.cfg.SCIM.TransformDefaults, scimConfigMap)
if err != nil {
return scim.Page{}, err
}
Expand Down
14 changes: 2 additions & 12 deletions pkg/app/handlers/groups/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func (u GroupResourceHandler) Patch(r *http.Request, id string, operations []sci
return scim.Resource{}, serrors.ScimErrorInternal
}

scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context())
scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context(), u.cfg.SCIM.SCIMConfigKey)
if err != nil {
return scim.Resource{}, err
}
scimConfig, err := convert.TransformConfigFromMap(u.cfg.SCIM.TransformDefaults, scimConfigMap)
scimConfig, err := convert.TransformConfigFromMap(&u.cfg.SCIM.TransformDefaults, scimConfigMap)
if err != nil {
return scim.Resource{}, err
}
Expand All @@ -50,8 +50,6 @@ func (u GroupResourceHandler) Patch(r *http.Request, id string, operations []sci
var attr scim.ResourceAttributes
oldAttr := converter.ObjectToResourceAttributes(getObjResp.Result)

// object := getObjResp.Result

for _, op := range operations {
switch op.Op {
case scim.PatchOperationAdd:
Expand All @@ -75,14 +73,6 @@ func (u GroupResourceHandler) Patch(r *http.Request, id string, operations []sci
if err != nil {
return scim.Resource{}, err
}
// object.Etag = getObjResp.Result.Etag
// resp, err := dirClient.Writer.SetObject(r.Context(), &dsw.SetObjectRequest{
// Object: object,
// })
// if err != nil {
// u.logger.Err(err).Msg("error setting object")
// return scim.Resource{}, err
// }

transformResult, err := convert.TransformResource(attr, scimConfig, "group")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/handlers/users/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func (u UsersResourceHandler) Create(r *http.Request, attributes scim.ResourceAt
return scim.Resource{}, serrors.ScimErrorInternal
}

scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context())
scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context(), u.cfg.SCIM.SCIMConfigKey)
if err != nil {
return scim.Resource{}, err
}
scimConfig, err := convert.TransformConfigFromMap(u.cfg.SCIM.TransformDefaults, scimConfigMap)
scimConfig, err := convert.TransformConfigFromMap(&u.cfg.SCIM.TransformDefaults, scimConfigMap)
if err != nil {
return scim.Resource{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/handlers/users/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func (u UsersResourceHandler) Delete(r *http.Request, id string) error {
return serrors.ScimErrorInternal
}

scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context())
scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context(), u.cfg.SCIM.SCIMConfigKey)
if err != nil {
return err
}
scimConfig, err := convert.TransformConfigFromMap(u.cfg.SCIM.TransformDefaults, scimConfigMap)
scimConfig, err := convert.TransformConfigFromMap(&u.cfg.SCIM.TransformDefaults, scimConfigMap)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/app/handlers/users/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func (u UsersResourceHandler) Get(r *http.Request, id string) (scim.Resource, er
return scim.Resource{}, serrors.ScimErrorInternal
}

scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context())
scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context(), u.cfg.SCIM.SCIMConfigKey)
if err != nil {
return scim.Resource{}, err
}
scimConfig, err := convert.TransformConfigFromMap(u.cfg.SCIM.TransformDefaults, scimConfigMap)
scimConfig, err := convert.TransformConfigFromMap(&u.cfg.SCIM.TransformDefaults, scimConfigMap)
if err != nil {
return scim.Resource{}, err
}
Expand Down Expand Up @@ -79,11 +79,11 @@ func (u UsersResourceHandler) GetAll(r *http.Request, params scim.ListRequestPar
return scim.Page{}, serrors.ScimErrorInternal
}

scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context())
scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context(), u.cfg.SCIM.SCIMConfigKey)
if err != nil {
return scim.Page{}, err
}
scimConfig, err := convert.TransformConfigFromMap(u.cfg.SCIM.TransformDefaults, scimConfigMap)
scimConfig, err := convert.TransformConfigFromMap(&u.cfg.SCIM.TransformDefaults, scimConfigMap)
if err != nil {
return scim.Page{}, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/app/handlers/users/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func (u UsersResourceHandler) Patch(r *http.Request, id string, operations []sci
return scim.Resource{}, serrors.ScimErrorInternal
}

scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context())
scimConfigMap, err := dirClient.GetTransformConfigMap(r.Context(), u.cfg.SCIM.SCIMConfigKey)
if err != nil {
return scim.Resource{}, err
}
scimConfig, err := convert.TransformConfigFromMap(u.cfg.SCIM.TransformDefaults, scimConfigMap)
scimConfig, err := convert.TransformConfigFromMap(&u.cfg.SCIM.TransformDefaults, scimConfigMap)
if err != nil {
return scim.Resource{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/app/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,5 @@ func NewLogger(l *zerolog.Logger) scimLogger {
}

func (l scimLogger) Error(args ...interface{}) {
l.log.Error().Any("args", args).Msg("error occured")
l.log.Error().Any("args", args).Msg("error occurred")
}
13 changes: 6 additions & 7 deletions pkg/common/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,19 @@ func HandlePatchOPReplace(objectProps scim.ResourceAttributes, op scim.PatchOper
var err error

if op.Path == nil {
switch value := op.Value.(type) {
case map[string]interface{}:
value, ok := op.Value.(map[string]interface{})
if ok {
for k, v := range value {
objectProps[k] = v
}
}
} else {

switch objectProps[op.Path.AttributePath.AttributeName].(type) {
switch value := objectProps[op.Path.AttributePath.AttributeName].(type) {
case string:
objectProps[op.Path.AttributePath.AttributeName] = op.Value
case map[string]interface{}:
if op.Path.AttributePath.SubAttribute != nil {
objectProps[op.Path.AttributePath.AttributeName].(map[string]interface{})[*op.Path.AttributePath.SubAttribute] = op.Value
value[*op.Path.AttributePath.SubAttribute] = op.Value
} else {
objectProps[op.Path.AttributePath.AttributeName] = op.Value
}
Expand All @@ -139,7 +138,7 @@ func HandlePatchOPReplace(objectProps scim.ResourceAttributes, op scim.PatchOper

index := -1
if ftr.Operator == filter.EQ {
for i, v := range objectProps[op.Path.AttributePath.AttributeName].([]interface{}) {
for i, v := range value {
originalValue := v.(map[string]interface{})
if originalValue[ftr.AttributePath.AttributeName].(string) == ftr.CompareValue {
index = i
Expand All @@ -148,7 +147,7 @@ func HandlePatchOPReplace(objectProps scim.ResourceAttributes, op scim.PatchOper
if index == -1 {
return nil, serrors.ScimErrorMutability
}
objectProps[op.Path.AttributePath.AttributeName].([]interface{})[index].(map[string]interface{})[*op.Path.SubAttribute] = op.Value
value[index].(map[string]interface{})[*op.Path.SubAttribute] = op.Value
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Config struct {
} `json:"server"`

SCIM struct {
SCIMConfigKey string `json:"scim_config_key"`
TransformDefaults TransformConfig `json:"transform_defaults"`
} `json:"scim"`
}
Expand Down Expand Up @@ -126,6 +127,8 @@ func NewConfig(configPath string, log *zerolog.Logger, certsGenerator *certs.Gen
v.SetDefault("server.certs.tls_cert_path", filepath.Join(DefaultTLSGenDir, "grpc.crt"))
v.SetDefault("server.certs.tls_ca_cert_path", filepath.Join(DefaultTLSGenDir, "grpc-ca.crt"))

v.SetDefault("scim.scim_config_key", "scim_config")

v.SetDefault("scim.transform_defaults.create_email_identities", true)
v.SetDefault("scim.transform_defaults.group_object_type", "group")
v.SetDefault("scim.transform_defaults.group_member_relation", "member")
Expand Down
4 changes: 1 addition & 3 deletions pkg/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ func (c *Converter) SCIMUserToObject(user *model.User) (*dsc.Object, error) {
displayName = userID
}

// props.Fields["enabled"] = structpb.NewBoolValue(user.Active)

object := &dsc.Object{
Type: c.cfg.SourceUserType,
Properties: props,
Expand Down Expand Up @@ -188,7 +186,7 @@ func ProtobufStructToMap(s *structpb.Struct) (map[string]interface{}, error) {
return m, nil
}

func TransformConfigFromMap(defaults config.TransformConfig, t map[string]interface{}) (*config.TransformConfig, error) {
func TransformConfigFromMap(defaults *config.TransformConfig, t map[string]interface{}) (*config.TransformConfig, error) {
cfg := &config.TransformConfig{
CreateEmailIdentities: defaults.CreateEmailIdentities,
CreateRoleGroups: defaults.CreateRoleGroups,
Expand Down
6 changes: 3 additions & 3 deletions pkg/directory/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func GetDirectoryClient(ctx context.Context, cfg *client.Config) (*DirectoryClie
}, nil
}

func (d *DirectoryClient) GetTransformConfigMap(ctx context.Context) (map[string]interface{}, error) {
func (d *DirectoryClient) GetTransformConfigMap(ctx context.Context, cfgKey string) (map[string]interface{}, error) {
varsResp, err := d.Reader.GetObject(ctx, &dsr3.GetObjectRequest{
ObjectType: "scim_config",
ObjectId: "scim_config",
ObjectType: cfgKey,
ObjectId: cfgKey,
})
if err != nil {
return nil, err
Expand Down
Loading