Skip to content

Commit

Permalink
Add session update to finetune probe calls (#53)
Browse files Browse the repository at this point in the history
* Updated probe calls and added cookie to client

* Updated configconnect object during BaisSystemInfo call

* added mutex lock & removed auth method

* fixed linter warnings

* fixed linter warnings at unityclient

---------

Co-authored-by: Indukuri <rajendra.indukuri@dell.com>
Co-authored-by: KshitijaKakde <111420075+KshitijaKakde@users.noreply.github.com>
Co-authored-by: Rishabh Raj <rishabh.raj2@dell.com>
  • Loading branch information
4 people authored Aug 14, 2024
1 parent 2cc3b38 commit 10cd868
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
4 changes: 1 addition & 3 deletions api/restclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func New(_ context.Context, host string, opts ClientOptions, debug bool) (Client

host = strings.Replace(host, "/api", "", 1)

cookieJar, _ := cookiejar.New(nil)
cookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: nil})

c := &client{
http: &http.Client{},
Expand All @@ -146,7 +146,6 @@ func New(_ context.Context, host string, opts ClientOptions, debug bool) (Client
if opts.Insecure {
c.http.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
/* #nosec G402 */
InsecureSkipVerify: true,
CipherSuites: util.GetSecuredCipherSuites(),
},
Expand All @@ -158,7 +157,6 @@ func New(_ context.Context, host string, opts ClientOptions, debug bool) (Client
}
c.http.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
/* #nosec G402 */
RootCAs: pool,
InsecureSkipVerify: false,
CipherSuites: util.GetSecuredCipherSuites(),
Expand Down
49 changes: 41 additions & 8 deletions unityclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http"
"os"
"strconv"
"sync"

"github.com/dell/gounity/util"

Expand All @@ -49,6 +50,7 @@ var (
type Client struct {
configConnect *ConfigConnect
api api.Client
loginMutex sync.Mutex
}

// ConfigConnect Struct holds the endpoint & credential info.
Expand All @@ -59,9 +61,47 @@ type ConfigConnect struct {
Insecure bool
}

// BasicSystemInfo make a REST API call [/basicSystemInfo/instances] to Unity to check if array is responding.
func (c *Client) BasicSystemInfo(ctx context.Context, configConnect *ConfigConnect) error {
log := util.GetRunIDLogger(ctx)
log.Debug("Executing BasicSystemInfo REST client")
c.configConnect = configConnect
headers := make(map[string]string, 3)
headers[api.XEmcRestClient] = "true"
headers[api.HeaderKeyContentType] = api.HeaderValContentTypeJSON
resp, err := c.api.DoAndGetResponseBody(ctx, http.MethodGet, api.UnityAPIBasicSysInfoURI, headers, nil)
if err != nil {
return fmt.Errorf("Error getting BasicSystemInfo: %v", err)
}

if resp != nil {
log.Debugf("BasicSystemInfo response code: %d", resp.StatusCode)
if err != nil {
log.Errorf("Reading BasicSystemInfo response body error:%v", err)
}

defer resp.Body.Close()

switch {
case resp.StatusCode >= 200 && resp.StatusCode <= 299:
{
log.Debug("Getting BasicSystemInfo details successful")
}
default:
return fmt.Errorf("Get BaicSystemInfo error. Response: %v", c.api.ParseJSONError(ctx, resp))
}

} else {
log.Errorf("Getting BasicSystenInfo details faile")
}
return nil
}

// Authenticate make a REST API call [/loginSessionInfo] to Unity to get authenticate the given credentials.
// The response contains the EMC-CSRF-TOKEN and the client caches it for further communication.
func (c *Client) Authenticate(ctx context.Context, configConnect *ConfigConnect) error {
c.loginMutex.Lock()
defer c.loginMutex.Unlock()
log := util.GetRunIDLogger(ctx)
log.Debug("Executing Authenticate REST client")
c.configConnect = configConnect
Expand Down Expand Up @@ -129,14 +169,7 @@ func (c *Client) executeWithRetryAuthenticate(ctx context.Context, method, uri s
if e.ErrorContent.HTTPStatusCode == 401 {
log.Debug("need to re-authenticate")
// Authenticate then try again
configConnect := c.configConnect
c, err = NewClientWithArgs(ctx, configConnect.Endpoint, configConnect.Insecure)
if err != nil {
log.Debug("Failed creating a new goUnity client during reauth when response code is 401 ")
return err
}

if err := c.Authenticate(ctx, configConnect); err != nil {
if err := c.Authenticate(ctx, c.configConnect); err != nil {
return fmt.Errorf("authentication failure due to: %v", err)
}
log.Debug("Authentication success")
Expand Down

0 comments on commit 10cd868

Please sign in to comment.