Skip to content

Commit

Permalink
feat: add api version parameter (#180)
Browse files Browse the repository at this point in the history
* feat: add api version parameter

* feat: improve api version definition
  • Loading branch information
btfhernandez authored Jan 13, 2025
1 parent 921b5b7 commit 6f5ccc3
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 63 deletions.
20 changes: 14 additions & 6 deletions TestClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func main() {
zapLogger := logging.NewZapLogger(logger)

apiUrl := "https://example.com:443/BeyondTrust/api/public/v3/"

// the recommended version is 3.1. If no version is specified,
// the default API version 3.0 will be used
apiVersion := "3.1"

clientId := ""
clientSecret := ""
separator := "/"
Expand Down Expand Up @@ -73,7 +78,7 @@ func main() {
httpClientObj, _ := utils.GetHttpClient(clientTimeOutInSeconds, verifyCa, certificate, certificateKey, zapLogger)

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

// authenticating
userObject, err := authenticate.GetPasswordSafeAuthentication()
Expand All @@ -84,23 +89,23 @@ func main() {
// instantiating secret obj
secretObj, _ := secrets.NewSecretObj(*authenticate, zapLogger, maxFileSecretSizeBytes)

secretPaths := []string{"fake/Client", "fake/test_file_1"}
secretPaths := []string{"oauthgrp/credential8", "oauthgrp/file1"}

gotSecrets, _ := secretObj.GetSecrets(secretPaths, separator)

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

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

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

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

newSecretPaths := []string{"fake/account01", "fake/account01"}
newSecretPaths := []string{"system01/managed_account01", "system01/managed_account02"}

//managedAccountList := strings.Split(paths, ",")
gotManagedAccounts, _ := manageAccountObj.GetSecrets(newSecretPaths, separator)
Expand All @@ -109,13 +114,13 @@ func main() {
zapLogger.Warn(fmt.Sprintf("%v", gotManagedAccounts))

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

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

account := entities.AccountDetails{
AccountName: "ManagedAccountTest",
AccountName: "ManagedAccountTest_" + uuid.New().String(),
Password: "Passw0rd101!*",
DomainName: "exampleDomain",
UserPrincipalName: "user@example.com",
Expand Down Expand Up @@ -167,6 +172,7 @@ func main() {
Notes: "My note",
Owners: []entities.OwnerDetails{
{
GroupId: 1,
OwnerId: userObject.UserId,
Owner: userObject.UserName,
Email: userObject.EmailAddress,
Expand Down Expand Up @@ -200,6 +206,7 @@ func main() {
FolderId: uuid.New(),
Owners: []entities.OwnerDetails{
{
GroupId: 1,
OwnerId: userObject.UserId,
Owner: userObject.UserName,
Email: userObject.EmailAddress,
Expand Down Expand Up @@ -231,6 +238,7 @@ func main() {
OwnerId: userObject.UserId,
Owners: []entities.OwnerDetails{
{
GroupId: 1,
OwnerId: userObject.UserId,
Owner: userObject.UserName,
Email: userObject.EmailAddress,
Expand Down
7 changes: 5 additions & 2 deletions api/authentication/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
// AuthenticationObj responsbile for authentication request data.
type AuthenticationObj struct {
ApiUrl url.URL
ApiVersion string
clientId string
clientSecret string
apiKey string
Expand All @@ -29,11 +30,12 @@ type AuthenticationObj struct {

// Authenticate is responsible for Auth configuration using Client Id and Client secret.
// Prerequisites - use input validation methods before using this class.
func Authenticate(httpClient utils.HttpClientObj, backoffDefinition *backoff.ExponentialBackOff, endpointUrl string, clientId string, clientSecret string, logger logging.Logger, retryMaxElapsedTimeSeconds int) (*AuthenticationObj, error) {
func Authenticate(httpClient utils.HttpClientObj, backoffDefinition *backoff.ExponentialBackOff, endpointUrl string, apiVersion string, clientId string, clientSecret string, logger logging.Logger, retryMaxElapsedTimeSeconds int) (*AuthenticationObj, error) {

apiUrl, _ := url.Parse(endpointUrl)
authenticationObj := &AuthenticationObj{
ApiUrl: *apiUrl,
ApiVersion: apiVersion,
HttpClient: httpClient,
clientId: clientId,
clientSecret: clientSecret,
Expand All @@ -48,11 +50,12 @@ func Authenticate(httpClient utils.HttpClientObj, backoffDefinition *backoff.Exp

// AuthenticateUsingApiKey is responsible for Auth configuration using API Key.
// Prerequisites - use input validation methods before using this class.
func AuthenticateUsingApiKey(httpClient utils.HttpClientObj, backoffDefinition *backoff.ExponentialBackOff, endpointUrl string, logger logging.Logger, retryMaxElapsedTimeSeconds int, apiKey string) (*AuthenticationObj, error) {
func AuthenticateUsingApiKey(httpClient utils.HttpClientObj, backoffDefinition *backoff.ExponentialBackOff, endpointUrl string, apiVersion string, logger logging.Logger, retryMaxElapsedTimeSeconds int, apiKey string) (*AuthenticationObj, error) {

apiUrl, _ := url.Parse(endpointUrl)
authenticationObj := &AuthenticationObj{
ApiUrl: *apiUrl,
ApiVersion: apiVersion,
HttpClient: httpClient,
clientId: "",
clientSecret: "",
Expand Down
14 changes: 9 additions & 5 deletions api/authentication/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type GetPasswordSafeAuthenticationConfig struct {
response *entities.SignApinResponse
}

// the recommended version is 3.1. If no version is specified,
// the default API version 3.0 will be used
var apiVersion string = "3.1"

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

Expand All @@ -60,7 +64,7 @@ func TestSignOut(t *testing.T) {
response: nil,
}

var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, testConfig.server.URL, "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, testConfig.server.URL, apiVersion, "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)

err := authenticate.SignOut()
if err != nil {
Expand All @@ -79,7 +83,7 @@ func TestSignAppin(t *testing.T) {
backoffDefinition := backoff.NewExponentialBackOff()
backoffDefinition.MaxElapsedTime = time.Second

var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", apiVersion, "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
testConfig := UserTestConfig{
name: "TestSignAppin",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -116,7 +120,7 @@ func TestSignAppinWithApiKey(t *testing.T) {
backoffDefinition := backoff.NewExponentialBackOff()
backoffDefinition.MaxElapsedTime = time.Second

var authenticate, _ = AuthenticateUsingApiKey(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", zapLogger, 300, "fake_api_key_")
var authenticate, _ = AuthenticateUsingApiKey(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", apiVersion, zapLogger, 300, "fake_api_key_")
testConfig := UserTestConfig{
name: "TestSignAppinWithApiKey",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -153,7 +157,7 @@ func TestGetToken(t *testing.T) {
backoffDefinition := backoff.NewExponentialBackOff()
backoffDefinition.MaxElapsedTime = time.Second

var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", apiVersion, "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
testConfig := GetTokenConfig{
name: "TestGetToken",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -195,7 +199,7 @@ func TestGetPasswordSafeAuthentication(t *testing.T) {
backoffDefinition := backoff.NewExponentialBackOff()
backoffDefinition.MaxElapsedTime = time.Second

var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
var authenticate, _ = Authenticate(*httpClientObj, backoffDefinition, "https://fake.api.com:443/BeyondTrust/api/public/v3/", apiVersion, "fakeone_a654+9sdf7+8we4f", "fakeone_aasd156465sfdef", zapLogger, 300)
testConfig := GetPasswordSafeAuthenticationConfig{
name: "TestGetPasswordSafeAuthentication",
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
1 change: 1 addition & 0 deletions api/entities/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ type SecretFileDetails struct {
}

type OwnerDetails struct {
GroupId int `json:",omitempty" validate:"required,min=1,max=2147483647"`
OwnerId int `json:",omitempty" validate:"required,min=1,max=2147483647"`
Owner string `json:",omitempty" validate:"omitempty"`
Email string `json:",omitempty" validate:"omitempty"`
Expand Down
Loading

0 comments on commit 6f5ccc3

Please sign in to comment.