This commit is contained in:
mr
2025-11-20 16:31:10 +01:00
parent a546c1220e
commit b154532a1a
2 changed files with 81 additions and 68 deletions

View File

@@ -280,7 +280,7 @@ func (cli *Client) connect(ctx context.Context) <-chan conn {
cn, err := cli.connector.Connect(ctx, addr) cn, err := cli.connector.Connect(ctx, addr)
if err != nil { 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 return
} }
select { select {

147
main.go
View File

@@ -19,6 +19,7 @@ import (
"cloud.o-forge.io/core/oc-lib/tools" "cloud.o-forge.io/core/oc-lib/tools"
beego "github.com/beego/beego/v2/server/web" beego "github.com/beego/beego/v2/server/web"
"github.com/beego/beego/v2/server/web/filter/cors" "github.com/beego/beego/v2/server/web/filter/cors"
"github.com/i-core/rlog"
) )
const appname = "oc-auth" const appname = "oc-auth"
@@ -79,21 +80,24 @@ func generateRole() {
}() }()
// if from ldap, create roles from ldap // if from ldap, create roles from ldap
if conf.GetConfig().SourceMode == "ldap" { if conf.GetConfig().SourceMode == "ldap" {
ldap := auth_connectors.New() for {
roles, err := ldap.GetRoles(context.Background()) ldap := auth_connectors.New()
if err == nil { roles, err := ldap.GetRoles(context.Background())
fmt.Println("ROLE", roles) if err == nil {
for _, role := range roles { fmt.Println("ROLE", roles)
for r, m := range role.Members { for _, role := range roles {
infrastructure.GetPermissionConnector("").CreateRole(r) for r, m := range role.Members {
for _, p := range m { infrastructure.GetPermissionConnector("").CreateRole(r)
infrastructure.GetPermissionConnector("").BindRole(r, p) 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) fmt.Println("Recovered in f", r)
} }
}() }()
// TODO check if files at private & public path are set log := rlog.FromContext(context.Background()).Sugar()
// check if files at private & public path are set for {
if _, err := os.Stat(conf.GetConfig().PrivateKeyPath); errors.Is(err, os.ErrNotExist) { // TODO check if files at private & public path are set
return errors.New("private key path does not exist") // check if files at private & public path are set
} if _, err := os.Stat(conf.GetConfig().PrivateKeyPath); errors.Is(err, os.ErrNotExist) {
if _, err := os.Stat(conf.GetConfig().PublicKeyPath); errors.Is(err, os.ErrNotExist) { return errors.New("private key path does not exist")
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")
} }
return nil if _, err := os.Stat(conf.GetConfig().PublicKeyPath); errors.Is(err, os.ErrNotExist) {
} return errors.New("public key path does not exist")
// create a new peer }
o := oclib.GetConfLoader() // check if peer already exists
peer := &peer.Peer{ p := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), "", "", []string{}, nil).Search(nil, strconv.Itoa(peer.SELF.EnumIndex()), false)
Url: o.GetStringDefault("HOSTNAME", "http://localhost"), file := ""
AbstractObject: utils.AbstractObject{ f, err := os.ReadFile(conf.GetConfig().PublicKeyPath)
Name: o.GetStringDefault("NAME", "local"), if err != nil {
}, time.Sleep(10 * time.Second)
PublicKey: file, log.Error(err)
State: peer.SELF, continue
WalletAddress: "my-wallet", }
} file = string(f)
data := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), "", "", []string{}, nil).StoreOne(peer.Serialize(peer)) if len(p.Data) > 0 {
if data.Err != "" { // check public key with the one in the database
time.Sleep(10 * time.Second) // Pause execution for 10 seconds // compare the public key from file with the one in the database
generateSelfPeer() if !strings.Contains(file, p.Data[0].(*peer.Peer).PublicKey) {
return errors.New(data.Err) 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 return nil
} }
@@ -154,23 +165,25 @@ func discovery() {
fmt.Println("Recovered in f", r) fmt.Println("Recovered in f", r)
} }
}() }()
api := tools.API{} for {
conn := infrastructure.GetPermissionConnector("") api := tools.API{}
fmt.Println("AdminRole", conn, conf.GetConfig().PermissionConnectorWriteHost) conn := infrastructure.GetPermissionConnector("")
_, _, err := conn.CreateRole(conf.GetConfig().AdminRole) fmt.Println("AdminRole", conn, conf.GetConfig().PermissionConnectorWriteHost)
if err != nil { _, _, err := conn.CreateRole(conf.GetConfig().AdminRole)
time.Sleep(10 * time.Second) // Pause execution for 10 seconds if err != nil {
discovery() time.Sleep(10 * time.Second) // Pause execution for 10 seconds
return continue
} }
conn.BindRole(conf.GetConfig().AdminRole, "admin") conn.BindRole(conf.GetConfig().AdminRole, "admin")
addPermissions := func(m map[string]interface{}) { addPermissions := func(m map[string]interface{}) {
for k, v := range m { for k, v := range m {
for _, p := range v.([]interface{}) { for _, p := range v.([]interface{}) {
conn.CreatePermission(k, p.(string), true) 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{}{})
} }