Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial commit #2

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions TestClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main

import (
"fmt"
"go-client-library-passwordsafe/api/authentication"
logging "go-client-library-passwordsafe/api/logging"
managed_accounts "go-client-library-passwordsafe/api/managed_account"
"go-client-library-passwordsafe/api/secrets"
"go-client-library-passwordsafe/api/utils"
"strings"

"go.uber.org/zap"
)

// main funtion
func main() {

//logFile, _ := os.Create("ProviderLogs.log")
//logger.SetOutput(logFile)

// create a zap logger
//logger, _ := zap.NewProduction()
logger, _ := zap.NewDevelopment()

// create a zap logger wrapper
zapLogger := logging.NewZapLogger(logger)

apiUrl := "https://example.com:443/BeyondTrust/api/public/v3/"
clientId := ""
clientSecret := ""
separator := "/"
certificate := ""
certificate_key := ""
clientTimeOutInSeconds := 5
verifyCa := true
maxElapsedTime := 15

// validate inputs
errors_in_inputs := utils.ValidateInputs(clientId, clientSecret, apiUrl, clientTimeOutInSeconds, &separator, verifyCa, zapLogger, certificate, certificate_key)

if errors_in_inputs != nil {
return
}

// creating a http client
httpClientObj, _ := utils.GetHttpClient(clientTimeOutInSeconds, verifyCa, certificate, certificate_key, zapLogger)

// instantiating authenticate obj, injecting httpClient object
authenticate, _ := authentication.Authenticate(*httpClientObj, apiUrl, clientId, clientSecret, zapLogger, maxElapsedTime)

// authenticating
_, err := authenticate.GetPasswordSafeAuthentication()
if err != nil {
return
}

// instantiating secret obj
secretObj, _ := secrets.NewSecretObj(*authenticate, zapLogger)

paths := "fake/text1,fake/text2"
errors_in_path := utils.ValidatePath(paths)
if errors_in_path != nil {
return
}

// getting secrets
secretList := strings.Split(paths, ",")
gotSecrets, _ := secretObj.GetSecrets(secretList, separator)

// WARNING: Do not log secrets in production code, the following log statement logs test secrets for testing purposes:
zapLogger.Info(fmt.Sprintf("%v", gotSecrets))

// getting single secret
gotSecret, _ := secretObj.GetSecret("fake/text1", separator)

// WARNING: Do not log secrets in production code, the following log statement logs test secrets for testing purposes:
zapLogger.Info(fmt.Sprintf("%v", gotSecret))

// instantiating managed account obj
manageAccountObj, _ := managed_accounts.NewManagedAccountObj(*authenticate, zapLogger)

paths = "fake/account01,fake/account02"
errors_in_path = utils.ValidatePath(paths)
if errors_in_path != nil {
return
}

managedAccountList := strings.Split(paths, ",")
gotManagedAccounts, _ := manageAccountObj.GetSecrets(managedAccountList, separator)

// WARNING: Do not log secrets in production code, the following log statement logs test secrets for testing purposes:
zapLogger.Info(fmt.Sprintf("%v", gotManagedAccounts))

// getting single managed account
gotManagedAccount, _ := manageAccountObj.GetSecret("fake/account01", separator)

// WARNING: Do not log secrets in production code, the following log statement logs test secrets for testing purposes:
zapLogger.Info(fmt.Sprintf("%v", gotManagedAccount))

// signing out
_ = authenticate.SignOut(fmt.Sprintf("%v%v", authenticate.ApiUrl, "Auth/Signout"))

}
184 changes: 184 additions & 0 deletions api/authentication/authentication_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Copyright 2024 BeyondTrust. All rights reserved.
// Package authentication implements functions to call Beyondtrust Secret Safe API.
// Unit tests for authentication package.
package authentication

import (
"go-client-library-passwordsafe/api/entities"
"go-client-library-passwordsafe/api/logging"
"go-client-library-passwordsafe/api/utils"
"reflect"

"net/http"
"net/http/httptest"
"testing"

"go.uber.org/zap"
)

type UserTestConfig struct {
name string
server *httptest.Server
response *entities.SignApinResponse
}

type GetTokenConfig struct {
name string
server *httptest.Server
response string
}

type GetPasswordSafeAuthenticationConfig struct {
name string
server *httptest.Server
response *entities.SignApinResponse
}

func TestSignOut(t *testing.T) {
logger, _ := zap.NewDevelopment()

// create a zap logger wrapper
zapLogger := logging.NewZapLogger(logger)

httpClientObj, _ := utils.GetHttpClient(5, false, "", "", zapLogger)

var authenticate, _ = Authenticate(*httpClientObj, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
testConfig := UserTestConfig{
name: "TestSignOut",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(``))
if err != nil {
t.Error("Test case Failed")
}

})),
response: nil,
}

err := authenticate.SignOut(testConfig.server.URL)
if err != nil {
t.Errorf("Test case Failed: %v", err)
}
}

func TestSignAppin(t *testing.T) {
logger, _ := zap.NewDevelopment()

// create a zap logger wrapper
zapLogger := logging.NewZapLogger(logger)

httpClientObj, _ := utils.GetHttpClient(5, false, "", "", zapLogger)

var authenticate, _ = Authenticate(*httpClientObj, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
testConfig := UserTestConfig{
name: "TestSignAppin",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`{"UserId":1, "EmailAddress":"Felipe"}`))
if err != nil {
t.Error("Test case Failed")
}
})),
response: &entities.SignApinResponse{
UserId: 1,
EmailAddress: "Felipe",
},
}

response, err := authenticate.SignAppin(testConfig.server.URL+"/"+"TestSignAppin", "")

if !reflect.DeepEqual(response, *testConfig.response) {
t.Errorf("Test case Failed %v, %v", response, *testConfig.response)
}

if err != nil {
t.Errorf("Test case Failed: %v", err)
}
}

func TestGetToken(t *testing.T) {
logger, _ := zap.NewDevelopment()

// create a zap logger wrapper
zapLogger := logging.NewZapLogger(logger)

httpClientObj, _ := utils.GetHttpClient(5, false, "", "", zapLogger)

var authenticate, _ = Authenticate(*httpClientObj, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
testConfig := GetTokenConfig{
name: "TestGetToken",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Mocking Response according to the endpoint path
switch r.URL.Path {

case "/Auth/connect/token":
_, err := w.Write([]byte(`{"access_token": "fake_token", "expires_in": 600, "token_type": "Bearer", "scope": "publicapi"}`))
if err != nil {
t.Error("Test case Failed")
}

default:
http.NotFound(w, r)
}
})),
response: "fake_token",
}

response, err := authenticate.GetToken(testConfig.server.URL+"/"+"Auth/connect/token", "", "")

if response != testConfig.response {
t.Errorf("Test case Failed %v, %v", response, testConfig.response)
}

if err != nil {
t.Errorf("Test case Failed: %v", err)
}
}

func TestGetPasswordSafeAuthentication(t *testing.T) {
logger, _ := zap.NewDevelopment()

// create a zap logger wrapper
zapLogger := logging.NewZapLogger(logger)

httpClientObj, _ := utils.GetHttpClient(5, false, "", "", zapLogger)

var authenticate, _ = Authenticate(*httpClientObj, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
testConfig := GetPasswordSafeAuthenticationConfig{
name: "TestGetPasswordSafeAuthentication",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Mocking Response according to the endpoint path
switch r.URL.Path {

case "/Auth/connect/token":
_, err := w.Write([]byte(`{"access_token": "fake_token", "expires_in": 600, "token_type": "Bearer", "scope": "publicapi"}`))
if err != nil {
t.Error("Test case Failed")
}

case "/Auth/SignAppIn":
_, err := w.Write([]byte(`{"UserId":1, "EmailAddress":"Felipe"}`))

if err != nil {
t.Error("Test case Failed")
}

default:
http.NotFound(w, r)
}
})),
response: &entities.SignApinResponse{
UserId: 1,
EmailAddress: "Felipe",
},
}
authenticate.ApiUrl = testConfig.server.URL + "/"
response, err := authenticate.GetPasswordSafeAuthentication()

if !reflect.DeepEqual(response, *testConfig.response) {
t.Errorf("Test case Failed %v, %v", response, *testConfig.response)
}

if err != nil {
t.Errorf("Test case Failed: %v", err)
}
}
Loading
Loading