convert metlusr to idntusr
This commit is contained in:
@@ -9,4 +9,7 @@ const (
|
|||||||
|
|
||||||
// IDPrefixUser defines the ID Prefix for a User.
|
// IDPrefixUser defines the ID Prefix for a User.
|
||||||
IDPrefixUser = "metlusr"
|
IDPrefixUser = "metlusr"
|
||||||
|
|
||||||
|
// IdentityPrefixUser defines the ID Prefix for a User created with Identity API.
|
||||||
|
IdentityPrefixUser = "idntusr"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,12 +1,19 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "go.infratographer.com/x/gidx"
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/base64"
|
||||||
|
|
||||||
|
"go.infratographer.com/x/gidx"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MetalUserPrefix = "metlusr"
|
MetalUserIssuer = "https://auth.equinix.com/"
|
||||||
|
MetalUserIssuerIDPrefix = "auth|"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserDetails struct {
|
type UserDetails struct {
|
||||||
|
id *gidx.PrefixedID
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
FullName string `json:"full_name"`
|
FullName string `json:"full_name"`
|
||||||
Organizations []*OrganizationDetails `json:"organizations"`
|
Organizations []*OrganizationDetails `json:"organizations"`
|
||||||
@@ -15,9 +22,37 @@ type UserDetails struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *UserDetails) PrefixedID() gidx.PrefixedID {
|
func (d *UserDetails) PrefixedID() gidx.PrefixedID {
|
||||||
if d.ID == "" {
|
if d.id != nil {
|
||||||
return gidx.NullPrefixedID
|
return *d.id
|
||||||
}
|
}
|
||||||
|
|
||||||
return gidx.PrefixedID(IDPrefixUser + "-" + d.ID)
|
nullID := gidx.NullPrefixedID
|
||||||
|
|
||||||
|
d.id = &nullID
|
||||||
|
|
||||||
|
if d.ID == "" {
|
||||||
|
return nullID
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := GenerateSubjectID(IdentityPrefixUser, MetalUserIssuer, MetalUserIssuerIDPrefix+d.ID)
|
||||||
|
if err != nil {
|
||||||
|
return nullID
|
||||||
|
}
|
||||||
|
|
||||||
|
d.id = &id
|
||||||
|
|
||||||
|
return *d.id
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateSubjectID(prefix, iss, sub string) (gidx.PrefixedID, error) {
|
||||||
|
// Concatenate the iss and sub values, then hash them
|
||||||
|
issSub := iss + sub
|
||||||
|
issSubHash := sha256.Sum256([]byte(issSub))
|
||||||
|
|
||||||
|
digest := base64.RawURLEncoding.EncodeToString(issSubHash[:])
|
||||||
|
|
||||||
|
// Concatenate the prefix with the digest
|
||||||
|
out := prefix + "-" + digest
|
||||||
|
|
||||||
|
return gidx.Parse(out)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,11 +6,13 @@ import (
|
|||||||
|
|
||||||
nc "github.com/nats-io/nats.go"
|
nc "github.com/nats-io/nats.go"
|
||||||
"go.infratographer.com/x/events"
|
"go.infratographer.com/x/events"
|
||||||
|
"go.infratographer.com/x/gidx"
|
||||||
"go.opentelemetry.io/otel"
|
"go.opentelemetry.io/otel"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
"go.opentelemetry.io/otel/trace"
|
"go.opentelemetry.io/otel/trace"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
|
"go.equinixmetal.net/infra9-metal-bridge/internal/metal/models"
|
||||||
"go.equinixmetal.net/infra9-metal-bridge/internal/service"
|
"go.equinixmetal.net/infra9-metal-bridge/internal/service"
|
||||||
|
|
||||||
"github.com/ThreeDotsLabs/watermill/message"
|
"github.com/ThreeDotsLabs/watermill/message"
|
||||||
@@ -173,7 +175,16 @@ func (s *Subscriber) handleTouchEvent(ctx context.Context, msg *message.Message,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.svc.IsUser(changeMsg.SubjectID) {
|
if s.svc.IsUser(changeMsg.SubjectID) {
|
||||||
if err := s.svc.AssignUser(ctx, changeMsg.SubjectID, changeMsg.AdditionalSubjectIDs...); err != nil {
|
userUUID := changeMsg.SubjectID.String()[gidx.PrefixPartLength+1:]
|
||||||
|
|
||||||
|
subjID, err := models.GenerateSubjectID(models.IdentityPrefixUser, models.MetalUserIssuer, models.MetalUserIssuerIDPrefix+userUUID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Errorw("failed to convert user id to identity id", "user.id", changeMsg.SubjectID.String(), "error", err)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.svc.AssignUser(ctx, subjID, changeMsg.AdditionalSubjectIDs...); err != nil {
|
||||||
// TODO: only return errors on retryable errors
|
// TODO: only return errors on retryable errors
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -206,7 +217,16 @@ func (s *Subscriber) handleDeleteEvent(ctx context.Context, msg *message.Message
|
|||||||
}
|
}
|
||||||
|
|
||||||
if s.svc.IsUser(changeMsg.SubjectID) {
|
if s.svc.IsUser(changeMsg.SubjectID) {
|
||||||
if err := s.svc.UnassignUser(ctx, changeMsg.SubjectID, changeMsg.AdditionalSubjectIDs...); err != nil {
|
userUUID := changeMsg.SubjectID.String()[gidx.PrefixPartLength+1:]
|
||||||
|
|
||||||
|
subjID, err := models.GenerateSubjectID(models.IdentityPrefixUser, models.MetalUserIssuer, models.MetalUserIssuerIDPrefix+userUUID)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.Errorw("failed to convert user id to identity id", "user.id", changeMsg.SubjectID.String(), "error", err)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.svc.UnassignUser(ctx, subjID, changeMsg.AdditionalSubjectIDs...); err != nil {
|
||||||
// TODO: only return errors on retryable errors
|
// TODO: only return errors on retryable errors
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user