Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
infamousjoeg committed Nov 17, 2023
1 parent 9de3924 commit 153b2b7
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@

.DS_Store
.dccache
bin/
bin/
testing*
5 changes: 2 additions & 3 deletions cmd/logon.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,14 @@ func logonToPAS(c pasapi.Client, username, password string, nonInteractive, conc
if err != nil && !strings.Contains(err.Error(), "ITATS542I") {
return fmt.Errorf("Failed to Logon to the PVWA. %s", err)
}
// Deal with OTPCode here if error contains challenge error code and redo client.Logon()
if err != nil {
// Get OTP code from Stdin
if strings.Contains(err.Error(), "ITATS542I") {
credentials, err = util.ReadOTPcode(credentials)
err = c.Logon(credentials)
if err != nil {
return fmt.Errorf("Failed to respond to challenge. Possible timeout occurred. %s", err)
}
}

// Set client config
err = c.SetConfig()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.18.10
github.com/aws/aws-sdk-go-v2/service/sts v1.18.2
github.com/cyberark/conjur-api-go v0.6.1
github.com/gorilla/sessions v1.2.2
github.com/quincycheng/cem-api-go v0.1.3
github.com/spf13/cobra v1.1.1
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand All @@ -97,6 +98,10 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
github.com/gorilla/sessions v1.2.2 h1:lqzMYz6bOfvn2WriPUjNByzeXIlVzURcPmgMczkmTjY=
github.com/gorilla/sessions v1.2.2/go.mod h1:ePLdVu+jbEgHH+KWw8I1z2wqd0BAdAQh/8LRvBeoNcQ=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down
8 changes: 7 additions & 1 deletion pkg/cybr/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ func (c *Client) Logon(req requests.Logon) error {
// Handle cyberark, ldap, and radius push, append & challenge/response authentication methods
url := fmt.Sprintf("%s/passwordvault/api/auth/%s/logon", c.BaseURL, c.AuthType)
token, err := httpJson.SendRequestRaw(false, url, "POST", "", req, c.InsecureTLS, c.Logger)
fmt.Printf("token: %s\n", string(token))
fmt.Printf("err: %s\n", err)
if err != nil {
return fmt.Errorf("Failed to authenticate to the PAS REST API. %s", err)
return fmt.Errorf("Failed to logon to PAS REST API Web Service. %s", err)
}
if strings.Contains(string(token), "ITATS542I") {
return fmt.Errorf("ITATS542I")
}

fmt.Printf("token: %s\n", strings.Trim(string(token), "\""))
c.SessionToken = strings.Trim(string(token), "\"")
return nil
}
Expand Down
29 changes: 23 additions & 6 deletions pkg/cybr/helpers/httpjson/httpjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"time"

"github.com/gorilla/sessions"
"github.com/infamousjoeg/cybr-cli/pkg/logger"
)

var (
store = sessions.NewCookieStore([]byte("2vQYfI$Qc$EJzqyI"))
)

func bodyToBytes(body interface{}) ([]byte, error) {
if body == nil {
return []byte(""), nil
Expand Down Expand Up @@ -53,7 +57,7 @@ func logRequest(req *http.Request, logger logger.Logger) {
body := buf.String()
logger.Writef("%s\n", body)

req.Body = ioutil.NopCloser(bytes.NewReader([]byte(body)))
req.Body = io.NopCloser(bytes.NewReader([]byte(body)))
}

func getResponse(identity bool, url string, method string, token string, body interface{}, insecureTLS bool, logger logger.Logger) (http.Response, error) {
Expand All @@ -73,7 +77,7 @@ func getResponse(identity bool, url string, method string, token string, body in
return *res, err
}

bodyReader = ioutil.NopCloser(bytes.NewReader(content))
bodyReader = io.NopCloser(bytes.NewReader(content))

// create the request
req, err := http.NewRequest(method, url, bodyReader)
Expand All @@ -96,14 +100,27 @@ func getResponse(identity bool, url string, method string, token string, body in
}
}

session, err := store.Get(req, "cybr-cli")
if err != nil {
return *res, fmt.Errorf("Failed to get session. %s", err)
}

for name, value := range session.Values {
req.AddCookie(&http.Cookie{Name: name.(string), Value: value.(string)})
}

logRequest(req, logger)
// send request
res, err = httpClient.Do(req)
if err != nil {
return http.Response{}, fmt.Errorf("Failed to send request. %s", err)
}

if res.StatusCode >= 300 {
for _, cookie := range res.Cookies() {
session.Values[cookie.Name] = cookie.Value
}

if res.StatusCode >= 300 && res.StatusCode != 500 {
return *res, fmt.Errorf("Received non-200 status code '%d'", res.StatusCode)
}

Expand Down Expand Up @@ -145,7 +162,7 @@ func SendRequestRaw(identity bool, url string, method string, token string, body
return nil, err
}

content, err := ioutil.ReadAll(res.Body)
content, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Failed to read body. %s", err)
}
Expand Down Expand Up @@ -191,7 +208,7 @@ func SendRequestRawWithHeaders(url, method string, headers http.Header, body int
return []byte(""), fmt.Errorf("Received non-200 status code '%d'", res.StatusCode)
}

content, err = ioutil.ReadAll(res.Body)
content, err = io.ReadAll(res.Body)
if err != nil {
return []byte(""), fmt.Errorf("Failed to read body. %s", err)
}
Expand Down

0 comments on commit 153b2b7

Please sign in to comment.