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: Optional services #29

Merged
merged 1 commit into from
Jun 25, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Issue # : Optional services based on supported version of keyhub


## [1.3.2] - 2024-06-12
### Changed
Expand Down
49 changes: 34 additions & 15 deletions keyhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package keyhub
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
Expand All @@ -40,6 +41,7 @@ type Client struct {
Vaults *VaultService
ServiceAccounts *ServiceAccountService
LaunchPadTile *LaunchPadTileService
VersionErrors []error
}

// khJsonBodyProvider encodes a JSON tagged struct value as a Body for requests.
Expand Down Expand Up @@ -74,23 +76,35 @@ func NewClientDefault(issuer string, clientID string, clientSecret string) (*Cli
func NewClient(httpClient *http.Client, issuer string, clientID string, clientSecret string) (*Client, error) {

var err error
var baseVersionedSupported bool
var newerVersionedSupported bool

base := sling.New().Client(httpClient).Base(issuer)

versionService := newVersionService(base.New().Set("Accept", "application/json").Set("Content-Type", "application/json"))

newClient := &Client{
ID: clientID,
Version: versionService,
VersionErrors: make([]error, 0),
}

// Create sling for contract version 60
baseVersionedSling := base.New()
_, err = versionService.CheckAndUpdateVersionedSling(60, baseVersionedSling)
baseVersionedSupported, err = versionService.CheckAndUpdateVersionedSling(60, baseVersionedSling)
if err != nil {
return nil, err
newClient.VersionErrors = append(newClient.VersionErrors, err)
}

// Create sling for contract version 71
newerVersionedSling := base.New()
_, err = versionService.CheckAndUpdateVersionedSling(71, newerVersionedSling)
newerVersionedSupported, err = versionService.CheckAndUpdateVersionedSling(71, newerVersionedSling)
if err != nil {
return nil, err
newClient.VersionErrors = append(newClient.VersionErrors, err)
}

if !(baseVersionedSupported && newerVersionedSupported) {
return nil, fmt.Errorf("KeyHub %v does not support api contract versions 60 or 71", versionService.info.KeyhubVersion)
}

ctx := oidc.ClientContext(context.Background(), httpClient)
Expand All @@ -115,15 +129,20 @@ func NewClient(httpClient *http.Client, issuer string, clientID string, clientSe
Base: oauth2Client.Transport,
},
}
return &Client{
ID: clientID,
Version: versionService,
Accounts: newAccountService(oauth2Sling.New()),
ClientApplications: newClientApplicationService(oauth2Sling.New()),
Groups: newGroupService(newerVersionedSling.New().Client(oauth2Client)),
Systems: newSystemService(oauth2Sling.New()),
LaunchPadTile: newLaunchPadTileService(oauth2Sling.New()),
Vaults: newVaultService(newerVersionedSling.New().Client(vaultClient)),
ServiceAccounts: NewServiceAccountService(oauth2Sling),
}, nil

if baseVersionedSupported {
newClient.Accounts = newAccountService(oauth2Sling.New())
newClient.ClientApplications = newClientApplicationService(oauth2Sling.New())
newClient.Systems = newSystemService(oauth2Sling.New())
newClient.LaunchPadTile = newLaunchPadTileService(oauth2Sling.New())
newClient.ServiceAccounts = NewServiceAccountService(oauth2Sling)
}

if newerVersionedSupported {
newClient.Groups = newGroupService(newerVersionedSling.New().Client(oauth2Client))
newClient.Vaults = newVaultService(newerVersionedSling.New().Client(vaultClient))
}

return newClient, nil

}
15 changes: 8 additions & 7 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,27 @@ func (s *VersionService) Get() (v *model.VersionInfo, err error) {
return results, nil
}

func (s *VersionService) CheckAndUpdateVersionedSling(version int, base *sling.Sling) (headerVersion string, err error) {
func (s *VersionService) CheckAndUpdateVersionedSling(version int, base *sling.Sling) (isSupported bool, err error) {

isSupported = false
var headerVersion string
if s.info == nil {
s.info, err = s.Get()
if err != nil {
return "", err
return
}
}

if version > 0 {

isContractVersionSupported := false
for _, contractVersion := range s.info.ContractVersions {
if version == contractVersion {
isContractVersionSupported = true
isSupported = true
break
}
}
if !isContractVersionSupported {
return "", fmt.Errorf("KeyHub %v does not support api contract version %v", s.info.KeyhubVersion, version)
if !isSupported {
return isSupported, fmt.Errorf("KeyHub %v does not support api contract version %v", s.info.KeyhubVersion, version)
}

headerVersion = fmt.Sprintf("%d", version)
Expand All @@ -89,5 +90,5 @@ func (s *VersionService) CheckAndUpdateVersionedSling(version int, base *sling.S
base.Set("Accept", fmt.Sprintf("%v;version=%s", mediatype, headerVersion))
base.Set("Content-Type", fmt.Sprintf("%v;version=%s", mediatype, headerVersion))

return
return isSupported, nil
}
Loading