Skip to content

Commit

Permalink
merged upstream changes from main
Browse files Browse the repository at this point in the history
  • Loading branch information
IliaRN committed Jan 26, 2022
1 parent 0aa0548 commit e1a5804
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 28 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Name | Description
`GOISILON_INSECURE` | whether to skip SSL validation
`GOISILON_VOLUMEPATH` | which base path to use when looking for volume directories
`GOISILON_VOLUMEPATH_PERMISSIONS` | permissions for new volume directory
`GOISILON_AUTHTYPE` | what should be the auth type, session-based or basic

### Initialize a new client with options
The following example demonstrates how to explicitly specify options when
Expand All @@ -53,7 +54,8 @@ client, err := NewClientWithArgs(
"groupName",
"password",
"/ifs/volumes",
"0777")
"0777",
0)
if err != nil {
panic(err)
}
Expand Down
40 changes: 29 additions & 11 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const (
headerISICSRFToken = "X-CSRF-Token"
headerISIReferer = "Referer"
isiSessCsrfToken = "Set-Cookie"
authTypeBasic = 0
authTypeSessionBased = 1
)

var (
Expand Down Expand Up @@ -145,6 +147,7 @@ type client struct {
apiMinorVersion uint8
verboseLogging VerboseType
sessionCredentials session
authType uint8
}

type session struct {
Expand Down Expand Up @@ -204,13 +207,18 @@ type ClientOptions struct {
func New(
ctx context.Context,
hostname, username, password, groupname string,
verboseLogging uint,
verboseLogging uint, authType uint8,
opts *ClientOptions) (Client, error) {

if hostname == "" || username == "" || password == "" {
return nil, errNewClient
}

if authType != authTypeBasic && authType != authTypeSessionBased {
log.Warn(ctx, "AuthType can be 0 or 1. Setting it to default value 0")
authType = authTypeBasic
}

c := &client{
hostname: hostname,
username: username,
Expand All @@ -219,6 +227,7 @@ func New(
volumePath: defaultVolumesPath,
volumePathPermissions: defaultVolumesPathPermissions,
verboseLogging: VerboseType(verboseLogging),
authType: authType,
}

c.http = &http.Client{}
Expand Down Expand Up @@ -258,7 +267,9 @@ func New(
}
}

c.authenticate(ctx, username, password, hostname)
if c.authType == authTypeSessionBased {
c.authenticate(ctx, username, password, hostname)
}
resp := &apiVerResponse{}
if err := c.Get(ctx, "/platform/latest", "", nil, nil, resp); err != nil &&
!strings.HasPrefix(err.Error(), "json: ") {
Expand Down Expand Up @@ -490,10 +501,14 @@ func (c *client) DoAndGetResponseBody(
}
}

if c.GetAuthToken() != "" {
req.Header.Set(headerISISessToken, c.GetAuthToken())
req.Header.Set(headerISIReferer, c.GetReferer())
req.Header.Set(headerISICSRFToken, c.GetCSRFToken())
if c.authType == authTypeBasic {
req.SetBasicAuth(c.username, c.password)
} else {
if c.GetAuthToken() != "" {
req.Header.Set(headerISISessToken, c.GetAuthToken())
req.Header.Set(headerISIReferer, c.GetReferer())
req.Header.Set(headerISICSRFToken, c.GetCSRFToken())
}
}

var (
Expand Down Expand Up @@ -636,6 +651,9 @@ func (c *client) authenticate(ctx context.Context, username string, password str
// it retries the same operation after performing authentication.
func (c *client) executeWithRetryAuthenticate(ctx context.Context, method, uri string, id string, params OrderedValues, headers map[string]string, body, resp interface{}) error {
err := c.DoWithHeaders(ctx, method, uri, id, params, headers, body, resp)
if c.authType == authTypeBasic {
return err
}
if err == nil {
log.Debug(ctx, "Execution successful on Method: %v, URI: %v", method, uri)
return nil
Expand All @@ -661,12 +679,12 @@ func (c *client) executeWithRetryAuthenticate(ctx context.Context, method, uri s

func FetchValueIndexForKey(l string, match string, sep string) (int, int, int) {

var startIndex, endIndex = -1, -1
if strings.Contains(l, match) {
if i := strings.Index(l, match); i != -1 {
if j := strings.Index(l[i+len(match):], sep); j != -1 {
return i, j, len(match)
}
startIndex = strings.Index(l, match)
if startIndex != -1 && sep != "" {
endIndex = strings.Index(l[startIndex+len(match):], sep)
}
}
return -1, -1, len(match)
return startIndex, endIndex, len(match)
}
12 changes: 8 additions & 4 deletions api/api_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,21 @@ func encryptPassword(buf []byte) []byte {
case strings.Contains(l, "password"):
match = `"password":"`
separator = `"`
case strings.Contains(l, "Cookie"):
match = `Cookie: isisessid=`
case strings.Contains(l, "Cookie: isisessid="):
match = `isisessid=`
separator = `-`
case strings.Contains(l, "X-Csrf-Token"):
match = `X-Csrf-Token:`
separator = `-`
case strings.Contains(l, "Authorization: Basic"):
match = `Basic `
}
if match != "" && separator != "" {
if match != "" {
startIndex, endIndex, matchStrLen := FetchValueIndexForKey(l, match, separator)
if startIndex > 0 || endIndex > 0 {
if startIndex >= 0 && endIndex > 0 { // if the separator is present then replace only the characters till separator with the special character
l = l[:startIndex+matchStrLen] + "****" + l[startIndex+matchStrLen+endIndex:]
} else if startIndex >= 0 { // if the separator in not present then replace the string to be masked with the special character
l = l[:startIndex+matchStrLen] + "****"
}
}
fmt.Fprintln(ou, l)
Expand Down
4 changes: 2 additions & 2 deletions api/v1/api_v1_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ func CopyIsiSnapshot(
func CopyIsiSnapshotWithIsiPath(
ctx context.Context,
client api.Client,
isiPath, sourceSnapshotName, sourceVolume, destinationName string) (resp *IsiVolume, err error) {
isiPath, snapshotSourceVolumeIsiPath, sourceSnapshotName, sourceVolume, destinationName string) (resp *IsiVolume, err error) {
// PAPI calls: PUT https://1.2.3.4:8080/namespace/path/to/volumes/destination_volume_name?merge=True
// x-isi-ifs-copy-source: /path/to/snapshot/volumes/source_volume_name
// x-isi-ifs-mode-mask: preserve
headers := map[string]string{
"x-isi-ifs-copy-source": path.Join(
"/",
GetRealVolumeSnapshotPathWithIsiPath(isiPath, sourceSnapshotName),
GetRealVolumeSnapshotPathWithIsiPath(snapshotSourceVolumeIsiPath, sourceSnapshotName),
sourceVolume),
"x-isi-ifs-mode-mask": "preserve",
}
Expand Down
22 changes: 15 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Copyright (c) 2019 Dell Inc, or its subsidiaries.
Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -12,7 +12,7 @@
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
*/
package goisilon

import (
Expand All @@ -33,7 +33,14 @@ type Client struct {

// NewClient returns a new Isilon client struct initialized from the environment.
func NewClient(ctx context.Context) (*Client, error) {
insecure, _ := strconv.ParseBool(os.Getenv("GOISILON_INSECURE"))
insecure, err := strconv.ParseBool(os.Getenv("GOISILON_INSECURE"))
if err != nil {
return nil, err
}
authType, err := strconv.Atoi(os.Getenv("GOISILON_AUTHTYPE"))
if err != nil {
return nil, err
}
return NewClientWithArgs(
ctx,
os.Getenv("GOISILON_ENDPOINT"),
Expand All @@ -43,21 +50,22 @@ func NewClient(ctx context.Context) (*Client, error) {
os.Getenv("GOISILON_GROUP"),
os.Getenv("GOISILON_PASSWORD"),
os.Getenv("GOISILON_VOLUMEPATH"),
os.Getenv("GOISILON_VOLUMEPATH_PERMISSIONS"))

os.Getenv("GOISILON_VOLUMEPATH_PERMISSIONS"),
uint8(authType),
)
}

// NewClientWithArgs returns a new Isilon client struct initialized from the supplied arguments.
func NewClientWithArgs(
ctx context.Context,
endpoint string,
insecure bool, verboseLogging uint,
user, group, pass, volumesPath string, volumesPathPermissions string) (*Client, error) {
user, group, pass, volumesPath string, volumesPathPermissions string, authType uint8) (*Client, error) {

timeout, _ := time.ParseDuration(os.Getenv("GOISILON_TIMEOUT"))

client, err := api.New(
ctx, endpoint, user, pass, group, verboseLogging,
ctx, endpoint, user, pass, group, verboseLogging, authType,
&api.ClientOptions{
Insecure: insecure,
VolumesPath: volumesPath,
Expand Down
1 change: 1 addition & 0 deletions env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ GOISILON_PASSWORD="admin"
GOISILON_INSECURE="true"
GOISILON_VOLUMEPATH="/ifs/data/csi"
GOISILON_VOLUMEPATH_PERMISSIONS="0777"
GOISILON_AUTHTYPE=0
5 changes: 4 additions & 1 deletion goisilon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func TestMain(m *testing.M) {
log.WithError(err).Panic(defaultCtx, "error fetching environment variable GOISILON_INSECURE")
}

authType, err := strconv.Atoi(os.Getenv("GOISILON_AUTHTYPE"))

client, err = NewClientWithArgs(
defaultCtx,
os.Getenv("GOISILON_ENDPOINT"),
Expand All @@ -74,7 +76,8 @@ func TestMain(m *testing.M) {
"",
os.Getenv("GOISILON_PASSWORD"),
os.Getenv("GOISILON_VOLUMEPATH"),
os.Getenv("GOISILON_VOLUMEPATH_PERMISSIONS"))
os.Getenv("GOISILON_VOLUMEPATH_PERMISSIONS"),
uint8(authType))

if err != nil {
log.WithError(err).Panic(defaultCtx, "error creating test client")
Expand Down
4 changes: 2 additions & 2 deletions snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c *Client) CopySnapshot(
// CopySnapshotWithIsiPath copies all files/directories in a snapshot with isiPath to a new directory.
func (c *Client) CopySnapshotWithIsiPath(
ctx context.Context,
isiPath string,
isiPath, snapshotSourceVolumeIsiPath string,
sourceID int64,
sourceName, destinationName string) (Volume, error) {

Expand All @@ -152,7 +152,7 @@ func (c *Client) CopySnapshotWithIsiPath(
}

_, err = api.CopyIsiSnapshotWithIsiPath(
ctx, c.API, isiPath, snapshot.Name,
ctx, c.API, isiPath, snapshotSourceVolumeIsiPath, snapshot.Name,
path.Base(snapshot.Path), destinationName)
if err != nil {
return nil, err
Expand Down

0 comments on commit e1a5804

Please sign in to comment.