From b154532a1acb2dace0d711df5c240c0777b11e41 Mon Sep 17 00:00:00 2001 From: mr Date: Thu, 20 Nov 2025 16:31:10 +0100 Subject: [PATCH] update --- infrastructure/auth_connector/ldap.go | 2 +- main.go | 147 ++++++++++++++------------ 2 files changed, 81 insertions(+), 68 deletions(-) diff --git a/infrastructure/auth_connector/ldap.go b/infrastructure/auth_connector/ldap.go index 32bec74..e32c46a 100644 --- a/infrastructure/auth_connector/ldap.go +++ b/infrastructure/auth_connector/ldap.go @@ -280,7 +280,7 @@ func (cli *Client) connect(ctx context.Context) <-chan conn { cn, err := cli.connector.Connect(ctx, addr) if err != nil { - fmt.Println("Failed to create a LDAP connection", "address", addr) + fmt.Println("Failed to create a LDAP connection", "address", addr, err) return } select { diff --git a/main.go b/main.go index 10217dd..87b0fc6 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "cloud.o-forge.io/core/oc-lib/tools" beego "github.com/beego/beego/v2/server/web" "github.com/beego/beego/v2/server/web/filter/cors" + "github.com/i-core/rlog" ) const appname = "oc-auth" @@ -79,21 +80,24 @@ func generateRole() { }() // if from ldap, create roles from ldap if conf.GetConfig().SourceMode == "ldap" { - ldap := auth_connectors.New() - roles, err := ldap.GetRoles(context.Background()) - if err == nil { - fmt.Println("ROLE", roles) - for _, role := range roles { - for r, m := range role.Members { - infrastructure.GetPermissionConnector("").CreateRole(r) - for _, p := range m { - infrastructure.GetPermissionConnector("").BindRole(r, p) + for { + ldap := auth_connectors.New() + roles, err := ldap.GetRoles(context.Background()) + if err == nil { + fmt.Println("ROLE", roles) + for _, role := range roles { + for r, m := range role.Members { + infrastructure.GetPermissionConnector("").CreateRole(r) + for _, p := range m { + infrastructure.GetPermissionConnector("").BindRole(r, p) + } } } + break + } else { + time.Sleep(10 * time.Second) // Pause execution for 10 seconds + continue } - } else { - time.Sleep(10 * time.Second) // Pause execution for 10 seconds - generateRole() } } } @@ -104,47 +108,54 @@ func generateSelfPeer() error { fmt.Println("Recovered in f", r) } }() - // TODO check if files at private & public path are set - // check if files at private & public path are set - if _, err := os.Stat(conf.GetConfig().PrivateKeyPath); errors.Is(err, os.ErrNotExist) { - return errors.New("private key path does not exist") - } - if _, err := os.Stat(conf.GetConfig().PublicKeyPath); errors.Is(err, os.ErrNotExist) { - return errors.New("public key path does not exist") - } - // check if peer already exists - p := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), "", "", []string{}, nil).Search(nil, strconv.Itoa(peer.SELF.EnumIndex()), false) - file := "" - f, err := os.ReadFile(conf.GetConfig().PublicKeyPath) - if err != nil { - return err - } - file = string(f) - if len(p.Data) > 0 { - // check public key with the one in the database - // compare the public key from file with the one in the database - if !strings.Contains(file, p.Data[0].(*peer.Peer).PublicKey) { - return errors.New("public key is different from the one in the database") + log := rlog.FromContext(context.Background()).Sugar() + for { + // TODO check if files at private & public path are set + // check if files at private & public path are set + if _, err := os.Stat(conf.GetConfig().PrivateKeyPath); errors.Is(err, os.ErrNotExist) { + return errors.New("private key path does not exist") } - return nil - } - // create a new peer - o := oclib.GetConfLoader() - peer := &peer.Peer{ - Url: o.GetStringDefault("HOSTNAME", "http://localhost"), - AbstractObject: utils.AbstractObject{ - Name: o.GetStringDefault("NAME", "local"), - }, - PublicKey: file, - State: peer.SELF, - WalletAddress: "my-wallet", - } - data := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), "", "", []string{}, nil).StoreOne(peer.Serialize(peer)) - if data.Err != "" { - time.Sleep(10 * time.Second) // Pause execution for 10 seconds - generateSelfPeer() - return errors.New(data.Err) + if _, err := os.Stat(conf.GetConfig().PublicKeyPath); errors.Is(err, os.ErrNotExist) { + return errors.New("public key path does not exist") + } + // check if peer already exists + p := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), "", "", []string{}, nil).Search(nil, strconv.Itoa(peer.SELF.EnumIndex()), false) + file := "" + f, err := os.ReadFile(conf.GetConfig().PublicKeyPath) + if err != nil { + time.Sleep(10 * time.Second) + log.Error(err) + continue + } + file = string(f) + if len(p.Data) > 0 { + // check public key with the one in the database + // compare the public key from file with the one in the database + if !strings.Contains(file, p.Data[0].(*peer.Peer).PublicKey) { + return errors.New("public key is different from the one in the database") + } + return nil + } + // create a new peer + o := oclib.GetConfLoader() + peer := &peer.Peer{ + Url: o.GetStringDefault("HOSTNAME", "http://localhost"), + AbstractObject: utils.AbstractObject{ + Name: o.GetStringDefault("NAME", "local"), + }, + PublicKey: file, + State: peer.SELF, + WalletAddress: "my-wallet", + } + data := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), "", "", []string{}, nil).StoreOne(peer.Serialize(peer)) + if data.Err != "" { + time.Sleep(10 * time.Second) // Pause execution for 10 seconds + log.Error(err) + continue + } + break } + return nil } @@ -154,23 +165,25 @@ func discovery() { fmt.Println("Recovered in f", r) } }() - api := tools.API{} - conn := infrastructure.GetPermissionConnector("") - fmt.Println("AdminRole", conn, conf.GetConfig().PermissionConnectorWriteHost) - _, _, err := conn.CreateRole(conf.GetConfig().AdminRole) - if err != nil { - time.Sleep(10 * time.Second) // Pause execution for 10 seconds - discovery() - return - } - conn.BindRole(conf.GetConfig().AdminRole, "admin") - addPermissions := func(m map[string]interface{}) { - for k, v := range m { - for _, p := range v.([]interface{}) { - conn.CreatePermission(k, p.(string), true) + for { + api := tools.API{} + conn := infrastructure.GetPermissionConnector("") + fmt.Println("AdminRole", conn, conf.GetConfig().PermissionConnectorWriteHost) + _, _, err := conn.CreateRole(conf.GetConfig().AdminRole) + if err != nil { + time.Sleep(10 * time.Second) // Pause execution for 10 seconds + continue + } + conn.BindRole(conf.GetConfig().AdminRole, "admin") + addPermissions := func(m map[string]interface{}) { + for k, v := range m { + for _, p := range v.([]interface{}) { + conn.CreatePermission(k, p.(string), true) + } } } + api.ListenRouter(addPermissions) + tools.NewNATSCaller().SetNATSPub("api", tools.DISCOVERY, map[string]interface{}{}) + break } - api.ListenRouter(addPermissions) - tools.NewNATSCaller().SetNATSPub("api", tools.DISCOVERY, map[string]interface{}{}) }