Skip to content

Commit

Permalink
Adding token
Browse files Browse the repository at this point in the history
  • Loading branch information
mchavez committed Jun 23, 2024
1 parent a91acc8 commit bd36c1c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
4 changes: 4 additions & 0 deletions cmd/baton-jenkins/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func validateConfig(ctx context.Context, cfg *config) error {
return fmt.Errorf("jenkins-baseurl must be provided")
}

if cfg.JenkinsUsername == "" {
return fmt.Errorf("jenkins-username must be provided")
}

if cfg.JenkinsToken == "" {
if cfg.JenkinsUsername == "" || cfg.JenkinsPassword == "" {
return fmt.Errorf("either bitbucketdc-token or (bitbucketdc-username/bitbucketdc-password) must be provided")
Expand Down
2 changes: 1 addition & 1 deletion cmd/baton-jenkins/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func getConnector(ctx context.Context, cfg *config) (types.ConnectorServer, erro
l := ctxzap.Extract(ctx)
jenkinsClient := client.NewClient()
if cfg.JenkinsToken != "" {
jenkinsClient.WithBearerToken(cfg.JenkinsToken)
jenkinsClient.WithUser(cfg.JenkinsUsername).WithBearerToken(cfg.JenkinsToken)
}

if cfg.JenkinsUsername != "" && cfg.JenkinsPassword != "" {
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ func WithSetBasicAuthHeader(username, password string) uhttp.RequestOption {
return uhttp.WithHeader("Authorization", "Basic "+basicAuth(username, password))
}

func WithSetBearerAuthHeader(token string) uhttp.RequestOption {
return uhttp.WithHeader("Authorization", "Bearer "+token)
func WithSetTokenAuthHeader(username, token string) uhttp.RequestOption {
return uhttp.WithHeader("Authorization", "Basic "+basicAuth(username, token))
}

func WithAuthorization(username, password, token string) uhttp.RequestOption {
if token != "" {
return WithSetBearerAuthHeader(token)
return WithSetTokenAuthHeader(username, token)
}

return WithSetBasicAuthHeader(username, password)
Expand Down
15 changes: 8 additions & 7 deletions pkg/client/internal_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ var (
ctx = context.Background()
userName, _ = os.LookupEnv("BATON_JENKINS_USERNAME")
password, _ = os.LookupEnv("BATON_JENKINS_PASSWORD")
token, _ = os.LookupEnv("BATON_JENKINS_TOKEN")
baseUrl = "http://localhost:8080"
)

func TestJenkinsClient_GetNodes(t *testing.T) {
if userName == "" && password == "" {
if userName == "" && password == "" && token == "" {
t.Skip()
}

Expand All @@ -29,7 +30,7 @@ func TestJenkinsClient_GetNodes(t *testing.T) {
}

func TestJenkinsClient_GetJobs(t *testing.T) {
if userName == "" && password == "" {
if userName == "" && password == "" && token == "" {
t.Skip()
}

Expand All @@ -49,15 +50,15 @@ func getJenkinsClientForTesting() *JenkinsClient {
auth: &auth{
user: userName,
password: password,
bearerToken: "",
bearerToken: token,
},
httpClient: getClientForTesting(ctx),
baseUrl: baseUrl,
}
}

func TestJenkinsClient_GetViews(t *testing.T) {
if userName == "" && password == "" {
if userName == "" && password == "" && token == "" {
t.Skip()
}

Expand All @@ -68,7 +69,7 @@ func TestJenkinsClient_GetViews(t *testing.T) {
}

func TestJenkinsClient_GetUsers(t *testing.T) {
if userName == "" && password == "" {
if userName == "" && password == "" && token == "" {
t.Skip()
}

Expand All @@ -79,7 +80,7 @@ func TestJenkinsClient_GetUsers(t *testing.T) {
}

func TestJenkinsClient_GetRoles(t *testing.T) {
if userName == "" && password == "" {
if userName == "" && password == "" && token == "" {
t.Skip()
}

Expand All @@ -101,7 +102,7 @@ func TestJenkinsClient_GetGroups(t *testing.T) {
}

func TestJenkinsClient_GetAllRoles(t *testing.T) {
if userName == "" && password == "" {
if userName == "" && password == "" && token == "" {
t.Skip()
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (d *Connector) Asset(ctx context.Context, asset *v2.AssetRef) (string, io.R
// Metadata returns metadata about the connector.
func (d *Connector) Metadata(ctx context.Context) (*v2.ConnectorMetadata, error) {
return &v2.ConnectorMetadata{
DisplayName: "My Baton Connector",
Description: "The template implementation of a baton connector",
DisplayName: "Jenkins Connector",
Description: "Connector syncing users, roles, groups, nodes and jobs from Jenkins.",
}, nil
}

Expand Down

0 comments on commit bd36c1c

Please sign in to comment.