Skip to content

Commit

Permalink
M #-: GOCA: Fixes for rpc/flow client credential parsers (#3076)
Browse files Browse the repository at this point in the history
  • Loading branch information
sk4zuzu committed May 28, 2024
1 parent f6fac11 commit c53e485
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 61 deletions.
49 changes: 26 additions & 23 deletions src/oca/go/src/goca/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
package goca

import (
"bufio"
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"

errs "github.com/OpenNebula/one/src/oca/go/src/goca/errors"

Expand Down Expand Up @@ -56,41 +57,43 @@ type Response struct {

// NewConfig returns a new OneConfig object with the specified user, password,
// and endpoint
func NewConfig(user string, password string, endpoint string) OneConfig {
var authToken string
var oneAuthPath string

oneXmlrpc := endpoint
func NewConfig(user, password, endpoint string) OneConfig {
var conf OneConfig

if user == "" && password == "" {
oneAuthPath = os.Getenv("ONE_AUTH")
oneAuthPath := os.Getenv("ONE_AUTH")
if oneAuthPath == "" {
oneAuthPath = os.Getenv("HOME") + "/.one/one_auth"
}

token, err := ioutil.ReadFile(oneAuthPath)
if err == nil {
authToken = strings.TrimSpace(string(token))
} else {
authToken = ""
file, err := os.Open(oneAuthPath)
if err != nil {
log.Fatalln(err)
}
} else {
authToken = user + ":" + password
}
defer file.Close()

scanner := bufio.NewScanner(file)

if oneXmlrpc == "" {
oneXmlrpc = os.Getenv("ONE_XMLRPC")
if oneXmlrpc == "" {
oneXmlrpc = "http://localhost:2633/RPC2"
scanner.Scan()
if scanner.Err() != nil {
log.Fatalln(scanner.Err())
}

conf.Token = scanner.Text()
} else {
conf.Token = user + ":" + password
}

config := OneConfig{
Token: authToken,
Endpoint: oneXmlrpc,
if endpoint == "" {
conf.Endpoint = os.Getenv("ONE_XMLRPC")
if conf.Endpoint == "" {
conf.Endpoint = "http://localhost:2633/RPC2"
}
} else {
conf.Endpoint = endpoint
}

return config
return conf
}

// NewDefaultClient return a new basic one client
Expand Down
76 changes: 38 additions & 38 deletions src/oca/go/src/goca/flow_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package goca

import (
"io/ioutil"
"bufio"
"log"
"net/http"
"os"
Expand All @@ -29,9 +29,9 @@ import (
// NewDefaultFlowClient return a basic RESTClient with flow information
func NewDefaultFlowClient(conf HTTPAuth) *RESTClient {
return &RESTClient{
user: conf.user,
pass: conf.pass,
address: conf.address,
user: conf.user,
pass: conf.pass,
address: conf.address,
httpClient: cleanhttp.DefaultPooledClient(),
}
}
Expand All @@ -43,58 +43,58 @@ func NewFlowClient(conf HTTPAuth, httpClient *http.Client) *RESTClient {
return NewDefaultFlowClient(conf)
}
return &RESTClient{
user: conf.user,
pass: conf.pass,
address: conf.address,
user: conf.user,
pass: conf.pass,
address: conf.address,
httpClient: httpClient,
}
}

// NewFlowConfig considering environment variables and such
func NewFlowConfig(fuser, fpass, fURL string) HTTPAuth {
// 1 - ONEFLOW_URL, ONEFLOW_USER and ONEFLOW_PASSWORD
// 2 - ONE_AUTH
// 3 - ~/.one/one_auth

// NewFlowConfig returns a new HTTPAuth object with the specified user, password,
// and endpoint.
func NewFlowConfig(user, password, endpoint string) HTTPAuth {
var conf HTTPAuth

if fURL == "" {
conf.address = os.Getenv("ONEFLOW_URL")

if conf.address == "" {
conf.address = "http://localhost:2474"
}
} else {
conf.address = fURL
}

if fuser == "" && fpass == "" {
if user == "" && password == "" {
oneAuthPath := os.Getenv("ONE_AUTH")
if oneAuthPath == "" {
oneAuthPath = os.Getenv("HOME") + "/.one/one_auth"
}

oneAuth, err := ioutil.ReadFile(oneAuthPath)
var auth string

if err == nil {
auth = string(oneAuth)
} else {
file, err := os.Open(oneAuthPath)
if err != nil {
log.Fatalln(err)
}
defer file.Close()

scanner := bufio.NewScanner(file)

scanner.Scan()
if scanner.Err() != nil {
log.Fatalln(scanner.Err())
}

parts := strings.Split(scanner.Text(), ":")
if len(parts) != 2 {
log.Fatalln("unable to parse credentials")
}

credentials := strings.Split(auth, ":")
conf.user = parts[0]
conf.pass = parts[1]

conf.user = credentials[0]
conf.pass = credentials[1]
} else {
conf.user = user
conf.pass = password
}

if endpoint == "" {
conf.address = os.Getenv("ONEFLOW_URL")
if conf.address == "" {
conf.address = "http://localhost:2474"
}
} else {
conf.user = fuser
conf.pass = fpass
conf.address = endpoint
}

return conf
}



0 comments on commit c53e485

Please sign in to comment.