Skip to content

Commit

Permalink
feat: Harvest should fetch certificates via a script
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrinds committed Jul 28, 2023
1 parent 7a1d84b commit 901c371
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 68 deletions.
29 changes: 14 additions & 15 deletions cmd/collectors/storagegrid/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ const (
)

type Client struct {
client *http.Client
request *http.Request
buffer *bytes.Buffer
Logger *logging.Logger
baseURL string
Cluster Cluster
username string
token string
Timeout time.Duration
logRest bool // used to log Rest request/response
APIPath string
auth *auth.Credentials
client *http.Client
request *http.Request
buffer *bytes.Buffer
Logger *logging.Logger
baseURL string
Cluster Cluster
token string
Timeout time.Duration
logRest bool // used to log Rest request/response
APIPath string
auth *auth.Credentials
}

type Cluster struct {
Expand Down Expand Up @@ -338,13 +337,13 @@ func (c *Client) fetchTokenWithAuthRetry() error {
if err != nil {
return fmt.Errorf("failed to create auth URL err: %w", err)
}
password, err := c.auth.Password()
pollerAuth, err := c.auth.GetPollerAuth()
if err != nil {
return err
}
authB := authBody{
Username: c.username,
Password: password,
Username: pollerAuth.Username,
Password: pollerAuth.Password,
}
postBody, err := json.Marshal(authB)
if err != nil {
Expand Down
63 changes: 24 additions & 39 deletions cmd/tools/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ const (
)

type Client struct {
client *http.Client
request *http.Request
buffer *bytes.Buffer
Logger *logging.Logger
baseURL string
cluster Cluster
username string
Timeout time.Duration
logRest bool // used to log Rest request/response
auth *auth.Credentials
client *http.Client
request *http.Request
buffer *bytes.Buffer
Logger *logging.Logger
baseURL string
cluster Cluster
Timeout time.Duration
logRest bool // used to log Rest request/response
auth *auth.Credentials
}

type Cluster struct {
Expand Down Expand Up @@ -81,13 +80,6 @@ func New(poller *conf.Poller, timeout time.Duration, auth *auth.Credentials) (*C
if err != nil {
return nil, err
}
pollerAuth, err := auth.GetPollerAuth()
if err != nil {
return nil, err
}
if !pollerAuth.IsCert {
client.username = pollerAuth.Username
}
transport.DialContext = (&net.Dialer{Timeout: DefaultDialerTimeout}).DialContext
httpclient = &http.Client{Transport: transport, Timeout: timeout}
client.client = httpclient
Expand Down Expand Up @@ -135,12 +127,12 @@ func (c *Client) GetRest(request string) ([]byte, error) {
return nil, err
}
c.request.Header.Set("accept", "application/json")
if c.username != "" {
password, err2 := c.auth.Password()
if err2 != nil {
return nil, err2
}
c.request.SetBasicAuth(c.username, password)
pollerAuth, err := c.auth.GetPollerAuth()
if err != nil {
return nil, err
}
if pollerAuth.Username != "" {
c.request.SetBasicAuth(pollerAuth.Username, pollerAuth.Password)
}
// ensure that we can change body dynamically
c.request.GetBody = func() (io.ReadCloser, error) {
Expand Down Expand Up @@ -232,11 +224,11 @@ func (c *Client) invokeWithAuthRetry() ([]byte, error) {
}
if pollerAuth.HasCredentialScript {
c.auth.Expire()
password, err2 := c.auth.Password()
pollerAuth2, err2 := c.auth.GetPollerAuth()
if err2 != nil {
return nil, err2
}
c.request.SetBasicAuth(pollerAuth.Username, password)
c.request.SetBasicAuth(pollerAuth2.Username, pollerAuth2.Password)
return doInvoke()
}
}
Expand All @@ -246,8 +238,6 @@ func (c *Client) invokeWithAuthRetry() ([]byte, error) {
}

func downloadSwagger(poller *conf.Poller, path string, url string, verbose bool) (int64, error) {
var restClient *Client

out, err := os.Create(path)
if err != nil {
return 0, fmt.Errorf("unable to create %s to save swagger.yaml", path)
Expand All @@ -259,23 +249,18 @@ func downloadSwagger(poller *conf.Poller, path string, url string, verbose bool)
}

timeout, _ := time.ParseDuration(DefaultTimeout)
if restClient, err = New(poller, timeout, auth.NewCredentials(poller, logging.Get())); err != nil {
return 0, fmt.Errorf("error creating new client %w", err)
credentials := auth.NewCredentials(poller, logging.Get())
transport, err := credentials.Transport(request)
if err != nil {
return 0, err
}
httpclient := &http.Client{Transport: transport, Timeout: timeout}

downClient := &http.Client{Transport: restClient.client.Transport, Timeout: restClient.client.Timeout}
if restClient.username != "" {
password, err2 := restClient.auth.Password()
if err2 != nil {
return 0, err2
}
request.SetBasicAuth(restClient.username, password)
}
if verbose {
requestOut, _ := httputil.DumpRequestOut(request, false)
fmt.Printf("REQUEST: %s BY: %s\n%s\n", url, restClient.username, requestOut)
fmt.Printf("REQUEST: %s\n%s\n", url, requestOut)
}
response, err := downClient.Do(request)
response, err := httpclient.Do(request)
if err != nil {
return 0, err
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/tools/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,11 @@ func FetchForCli(client *Client, href string, records *[]any, downloadAll bool,
return fmt.Errorf("error making request %w", err)
}

*curls = append(*curls, fmt.Sprintf("curl --user %s --insecure '%s%s'", client.username, client.baseURL, href))
pollerAuth, err := client.auth.GetPollerAuth()
if err != nil {
return err
}
*curls = append(*curls, fmt.Sprintf("curl --user %s --insecure '%s%s'", pollerAuth.Username, client.baseURL, href))

isNonIterRestCall := false
value := gjson.GetBytes(getRest, "records")
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/ontapi/zapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,11 +432,11 @@ func (c *Client) invokeWithAuthRetry(withTimers bool) (*node.Node, time.Duration
// and try again
if errors.Is(he, errs.ErrAuthFailed) && pollerAuth.HasCredentialScript {
c.auth.Expire()
password, err := c.auth.Password()
if err != nil {
return nil, 0, 0, err
pollerAuth2, err2 := c.auth.GetPollerAuth()
if err2 != nil {
return nil, 0, 0, err2
}
c.request.SetBasicAuth(pollerAuth.Username, password)
c.request.SetBasicAuth(pollerAuth2.Username, pollerAuth2.Password)
c.request.Body = io.NopCloser(&buffer)
c.request.ContentLength = int64(buffer.Len())
result2, s1, s2, err3 := c.invoke(withTimers)
Expand Down
13 changes: 4 additions & 9 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ type Credentials struct {
cachedPassword string
}

func (c *Credentials) Password() (string, error) {
auth, err := c.GetPollerAuth()
if err != nil {
return "", err
}
return auth.Password, nil
}

// Expire will reset the credential schedule if the receiver has a CredentialsScript
// Otherwise it will do nothing.
// Resetting the schedule will cause the next call to Password to fetch the credentials
Expand Down Expand Up @@ -317,10 +309,13 @@ func handCertificateAuth(c *Credentials, poller *conf.Poller, insecureTLS bool)
certPath := poller.SslCert
keyPath := poller.SslKey

if certPath == "" {
if certPath == "" || keyPath == "" {
o := &options.Options{}
options.SetPathsAndHostname(o)
pathPrefix = path.Join(o.HomePath, "cert/", o.Hostname)
}

if certPath == "" {
certPath = pathPrefix + ".pem"
}
if keyPath == "" {
Expand Down

0 comments on commit 901c371

Please sign in to comment.