Skip to content

Commit

Permalink
azmcmd
Browse files Browse the repository at this point in the history
  • Loading branch information
gertd committed Oct 23, 2023
1 parent 396ce2f commit 7fdedc0
Show file tree
Hide file tree
Showing 15 changed files with 982 additions and 202 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

.dev/
.ext/
.vscode/launch.json

bin/
dist/
Expand Down
18 changes: 0 additions & 18 deletions .vscode/launch.json

This file was deleted.

11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"cSpell.words": [
"aserto",
"etag",
"jsondiff",
"protobuf",
"samber",
"stretchr",
"timestamppb"
]
}
127 changes: 12 additions & 115 deletions cmd/azmcmd/cmds/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,12 @@ package cmds

import (
"context"
"fmt"
"io"
"os"

"github.com/aserto-dev/azm/migrate"
client "github.com/aserto-dev/go-aserto/client"
dsc2 "github.com/aserto-dev/go-directory/aserto/directory/common/v2"
dse2 "github.com/aserto-dev/go-directory/aserto/directory/exporter/v2"
dsr2 "github.com/aserto-dev/go-directory/aserto/directory/reader/v2"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/types/known/timestamppb"
)

const pageSize int32 = 100

type ObjRelSub struct {
Object string
Relation string
Subject string
}

func (ors ObjRelSub) Key() string {
return ors.Object + "|" + ors.Relation + "|" + ors.Subject
}

type MigrateCmd struct {
}

Expand All @@ -43,109 +26,23 @@ func (a *MigrateCmd) Run(c *Common) error {
return err
}

e2 := dse2.NewExporterClient(clnt.Conn)
r2 := dsr2.NewReaderClient(clnt.Conn)

ots := a.getObjectTypes(ctx, r2)

rts := a.getRelationTypes(ctx, r2)

ors, rsc := a.getObjectRelationSubject(ctx, e2)
m := migrate.NewMigrator()

fmt.Println("rts", len(rts))
fmt.Println("ots", len(ots))
fmt.Println("rsc", rsc)
fmt.Println("ors", len(ors))

for i, r := range ors {
fmt.Printf("%d %s %s %s\n", i, r.Object, r.Relation, r.Subject)
}

return nil
}

func (a *MigrateCmd) getObjectTypes(ctx context.Context, r2 dsr2.ReaderClient) []*dsc2.ObjectType {
token := ""
results := []*dsc2.ObjectType{}
for {
resp, err := r2.GetObjectTypes(ctx, &dsr2.GetObjectTypesRequest{Page: &dsc2.PaginationRequest{Size: pageSize, Token: token}})
if err != nil {
log.Error().Err(err).Msg("GetObjectTypes")
return []*dsc2.ObjectType{}
}
results = append(results, resp.Results...)
if resp.Page.NextToken == "" {
break
}
token = resp.Page.NextToken
}
return results
}

func (a *MigrateCmd) getRelationTypes(ctx context.Context, r2 dsr2.ReaderClient) []*dsc2.RelationType {
token := ""
results := []*dsc2.RelationType{}
for {
resp, err := r2.GetRelationTypes(ctx, &dsr2.GetRelationTypesRequest{Page: &dsc2.PaginationRequest{Size: pageSize, Token: token}})
if err != nil {
log.Error().Err(err).Msg("GetRelationTypes")
return []*dsc2.RelationType{}
}
results = append(results, resp.Results...)
if resp.Page.NextToken == "" {
break
}
token = resp.Page.NextToken
if err := m.Process(clnt.Conn); err != nil {
return err
}
return results
}

func (a *MigrateCmd) getObjectRelationSubject(ctx context.Context, e2 dse2.ExporterClient) ([]*ObjRelSub, int) {
stream, err := e2.Export(ctx, &dse2.ExportRequest{
Options: uint32(dse2.Option_OPTION_DATA_RELATIONS),
StartFrom: &timestamppb.Timestamp{},
})
if err != nil {
log.Error().Err(err).Msg("ExportRequest")
return []*ObjRelSub{}, 0
if err := m.Normalize(); err != nil {
return err
}

rsc := 0
orsMap := map[string]*ObjRelSub{}

for {
msg, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
log.Error().Err(err).Msg("Recv")
return []*ObjRelSub{}, 0
}

r, ok := msg.Msg.(*dse2.ExportResponse_Relation)
if !ok {
log.Warn().Msg("unknown message type, skipped")
continue
}

rsc++

i := ObjRelSub{
Object: r.Relation.Object.GetType(),
Relation: r.Relation.Relation,
Subject: r.Relation.Subject.GetType(),
}

if _, ok := orsMap[i.Key()]; !ok {
orsMap[i.Key()] = &i
}
if err := m.Validate(); err != nil {
return err
}

results := []*ObjRelSub{}
for _, v := range orsMap {
results = append(results, v)
if err := m.Write(os.Stdout); err != nil {
return err
}

return results, rsc
return nil
}
33 changes: 33 additions & 0 deletions migrate/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package migrate

import dsc2 "github.com/aserto-dev/go-directory/aserto/directory/common/v2"

type Metadata struct {
ObjectTypes []*dsc2.ObjectType
RelationTypes []*dsc2.RelationType
Permissions []*dsc2.Permission
}

const Obsolete int32 = 16

var RefObjectTypes = map[string]*dsc2.ObjectType{
"system": {Name: "system", DisplayName: "System", IsSubject: false, Ordinal: 6, Status: uint32(dsc2.Flag_FLAG_HIDDEN | dsc2.Flag_FLAG_SYSTEM | dsc2.Flag(Obsolete))},
"user": {Name: "user", DisplayName: "User", IsSubject: true, Ordinal: 1, Status: uint32(dsc2.Flag_FLAG_SYSTEM)},
"identity": {Name: "identity", DisplayName: "Identity", IsSubject: false, Ordinal: 2, Status: uint32(dsc2.Flag_FLAG_SYSTEM | dsc2.Flag_FLAG_READONLY)},
"group": {Name: "group", DisplayName: "Group", IsSubject: true, Ordinal: 3, Status: uint32(dsc2.Flag_FLAG_SYSTEM)},
"application": {Name: "application", DisplayName: "Application", IsSubject: false, Ordinal: 4, Status: uint32(dsc2.Flag_FLAG_SYSTEM | dsc2.Flag(Obsolete))},
"resource": {Name: "resource", DisplayName: "Resource", IsSubject: false, Ordinal: 5, Status: uint32(dsc2.Flag_FLAG_SYSTEM | dsc2.Flag(Obsolete))},
"user-v1": {Name: "user-v1", DisplayName: "UserV1", IsSubject: true, Ordinal: 0, Status: uint32(dsc2.Flag_FLAG_HIDDEN | dsc2.Flag_FLAG_SYSTEM | dsc2.Flag_FLAG_SHADOW | dsc2.Flag_FLAG_READONLY | dsc2.Flag(Obsolete))},
}

var RefRelationTypes = map[string]map[string]*dsc2.RelationType{
"system": {"user": {ObjectType: "system", Name: "user", DisplayName: "system#user", Ordinal: 0, Status: uint32(dsc2.Flag_FLAG_SYSTEM | dsc2.Flag(Obsolete))}},
"identity": {"identifier": {ObjectType: "identity", Name: "identifier", Unions: []string{"user"}, DisplayName: "identity#identifier", Ordinal: 0, Status: uint32(dsc2.Flag_FLAG_SYSTEM)}},
"group": {"member": {ObjectType: "group", Name: "member", Unions: []string{"user"}, DisplayName: "group#member", Ordinal: 0, Status: uint32(dsc2.Flag_FLAG_SYSTEM)}},
"application": {"user": {ObjectType: "application", Name: "user", DisplayName: "application#user", Ordinal: 0, Status: uint32(dsc2.Flag_FLAG_SYSTEM | dsc2.Flag(Obsolete))}},
"user": {"manager": {ObjectType: "user", Name: "manager", Unions: []string{"user"}, DisplayName: "user#manager", Ordinal: 0, Status: uint32(dsc2.Flag_FLAG_SYSTEM)}},
}

func IsObsolete(status dsc2.Flag) bool {
return status&dsc2.Flag(Obsolete) != 0
}
Loading

0 comments on commit 7fdedc0

Please sign in to comment.