add(rbac): initial rbac import
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package rbac
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
Client struct {
|
||||
Client *http.Client
|
||||
|
||||
isDebug bool
|
||||
config configuration
|
||||
}
|
||||
)
|
||||
|
||||
func New() (*Client, error) {
|
||||
if err := config.validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{
|
||||
Client: &http.Client{}, // @todo: timeouts
|
||||
isDebug: false,
|
||||
config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) Get(url string) (*http.Response, error) {
|
||||
return c.Request("GET", url)
|
||||
}
|
||||
|
||||
func (c *Client) Request(method string, url string) (*http.Response, error) {
|
||||
link := strings.TrimRight(c.config.baseURL, "/") + "/" + strings.TrimLeft(url, "/")
|
||||
|
||||
if c.isDebug {
|
||||
fmt.Println("RBAC >>> ", link)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, link, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(c.config.auth)))
|
||||
req.Header.Add("X-TENANT-ID", c.config.tenant)
|
||||
|
||||
resp, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package rbac
|
||||
|
||||
import (
|
||||
"github.com/namsral/flag"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type (
|
||||
configuration struct {
|
||||
auth string
|
||||
tenant string
|
||||
baseURL string
|
||||
}
|
||||
)
|
||||
|
||||
var config configuration
|
||||
|
||||
func (c configuration) validate() error {
|
||||
if c.auth == "" {
|
||||
return errors.New("No authentication provided for RBAC")
|
||||
}
|
||||
if c.tenant == "" {
|
||||
return errors.New("No tenant provided for RBAC")
|
||||
}
|
||||
if c.baseURL == "" {
|
||||
return errors.New("No Base URL provided for RBAC")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Flags should be called from main to register flags
|
||||
func Flags() {
|
||||
flag.StringVar(&config.auth, "rbac-auth", "username:password", "Credentials to use for RBAC queries")
|
||||
flag.StringVar(&config.tenant, "rbac-tenant", "", "Tenant ID")
|
||||
flag.StringVar(&config.baseURL, "rbac-base-url", "", "RBAC Base URL")
|
||||
}
|
||||
Reference in New Issue
Block a user