From 554fe5891fd3be71f6d99a5a2f90d61128eb33c7 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 16 Nov 2026 21:52:59 +1100 Subject: [PATCH] feat(connectors/servicenow): declare the api credential slot and support bearer auth The manifest now carries an authority block naming the api credential service, so the marketplace view shows the slot instead of relying on the description text. The client takes the full Authorization header: the api slot accepts a basic username/password or a bearer token, setting both is an error. Egress stays operator-supplied at accept time because the instance host comes from the instance_url setting, which a static egressHosts list cannot express. --- connectors/servicenow/README.md | 8 +++-- connectors/servicenow/client.go | 11 ++++-- connectors/servicenow/package.yaml | 5 ++- connectors/servicenow/servicenow.go | 18 ++++++++-- connectors/servicenow/servicenow_test.go | 43 ++++++++++++++++++------ 5 files changed, 65 insertions(+), 20 deletions(-) diff --git a/connectors/servicenow/README.md b/connectors/servicenow/README.md index 46670e8..378924f 100644 --- a/connectors/servicenow/README.md +++ b/connectors/servicenow/README.md @@ -23,9 +23,11 @@ Settings (see `package.yaml`): - `instance_url` (required): `https://.service-now.com`. - `poll_interval_seconds`: how often events are polled. Defaults to 30. -Credentials: the `api` slot takes a `basic` credential (username and password), -which the host resolves to `api.username` and `api.password`. Egress should be -confined to the instance host. +Credentials: the `api` slot takes a `basic` credential (username and password, +resolved to `api.username` and `api.password`) or a `bearer` credential (a +token, resolved to `api.token`); setting both is an error. The slot is declared +in `package.yaml` under `authority.credentialServices`. Egress is confined to +the instance host, entered when the installation is accepted. ## Deliveries diff --git a/connectors/servicenow/client.go b/connectors/servicenow/client.go index f741f76..7750118 100644 --- a/connectors/servicenow/client.go +++ b/connectors/servicenow/client.go @@ -22,14 +22,21 @@ type client struct { http *http.Client } -func newClient(instanceURL, username, password string) *client { +// newClient takes the full Authorization header value, so callers pick the +// scheme: basicAuth for a username and password, or "Bearer "+token. +func newClient(instanceURL, authHeader string) *client { return &client{ base: strings.TrimRight(instanceURL, "/"), - auth: "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password)), + auth: authHeader, http: &http.Client{Timeout: 30 * time.Second}, } } +// basicAuth builds the Authorization header for a basic credential. +func basicAuth(username, password string) string { + return "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password)) +} + // tableResult is the Table API's envelope: every read returns result rows. type tableResult struct { Result []map[string]any `json:"result"` diff --git a/connectors/servicenow/package.yaml b/connectors/servicenow/package.yaml index 717f043..332aff6 100644 --- a/connectors/servicenow/package.yaml +++ b/connectors/servicenow/package.yaml @@ -1,7 +1,10 @@ apiVersion: dev.archie.package.v1 displayName: ServiceNow -description: Records incidents and change requests on a ServiceNow instance, hears change approvals and rejections back as events, and answers CMDB lookups (owner, delegate, support group, environment, data class) plus change-calendar blackouts for the Timing guard. Settings name the instance URL; the api credential slot holds a basic username and password. +description: Records incidents and change requests on a ServiceNow instance, hears change approvals and rejections back as events, and answers CMDB lookups (owner, delegate, support group, environment, data class) plus change-calendar blackouts for the Timing guard. Settings name the instance URL; the api credential slot holds a basic username and password or a bearer token. Egress is the instance host, entered when the installation is accepted. version: 0.1.0 +authority: + credentialServices: + - api settings: - key: instance_url label: Instance URL diff --git a/connectors/servicenow/servicenow.go b/connectors/servicenow/servicenow.go index 11103f7..edcb499 100644 --- a/connectors/servicenow/servicenow.go +++ b/connectors/servicenow/servicenow.go @@ -30,9 +30,21 @@ func newServiceNow(cfg connector.Config) (*servicenow, error) { if err != nil || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" { return nil, fmt.Errorf("instance_url %q must be an http or https URL", base) } + token := cfg.Secrets["api.token"] username, password := cfg.Secrets["api.username"], cfg.Secrets["api.password"] - if username == "" || password == "" { - return nil, errors.New("the api credential slot needs a basic username and password") + basic := username != "" || password != "" + var auth string + switch { + case token != "" && basic: + return nil, errors.New("the api credential slot takes a basic username and password or a bearer token, not both") + case token != "": + auth = "Bearer " + token + case basic && (username == "" || password == ""): + return nil, errors.New("the api credential slot needs both a username and a password for basic auth") + case !basic: + return nil, errors.New("the api credential slot needs a basic username and password or a bearer token") + default: + auth = basicAuth(username, password) } poll := 30 * time.Second if s := cfg.Settings["poll_interval_seconds"]; s != "" { @@ -42,7 +54,7 @@ func newServiceNow(cfg connector.Config) (*servicenow, error) { } poll = time.Duration(n) * time.Second } - return &servicenow{client: newClient(base, username, password), poll: poll}, nil + return &servicenow{client: newClient(base, auth), poll: poll}, nil } // Describe declares everything the connector does, gathered from each area so diff --git a/connectors/servicenow/servicenow_test.go b/connectors/servicenow/servicenow_test.go index 8453053..c616022 100644 --- a/connectors/servicenow/servicenow_test.go +++ b/connectors/servicenow/servicenow_test.go @@ -167,28 +167,49 @@ func raw(v any) jsontext.Value { return jsontext.Value(b) } -func TestNewServiceNowRequiresInstanceAndBasicCredential(t *testing.T) { +func TestNewServiceNowConfig(t *testing.T) { if _, err := newServiceNow(connector.Config{Settings: map[string]string{}, Secrets: map[string]string{}}); err == nil { t.Fatal("expected an error without instance_url") } - if _, err := newServiceNow(connector.Config{ - Settings: map[string]string{"instance_url": "https://dev12345.service-now.com"}, - Secrets: map[string]string{}, - }); err == nil { + base := map[string]string{"instance_url": "https://dev12345.service-now.com/"} + if _, err := newServiceNow(connector.Config{Settings: base, Secrets: map[string]string{}}); err == nil { t.Fatal("expected an error without the api credential") } - s, err := newServiceNow(connector.Config{ + if _, err := newServiceNow(connector.Config{Settings: base, Secrets: map[string]string{ + "api.username": "admin", "api.token": "tok", + }}); err == nil { + t.Fatal("expected an error when both basic and bearer are set") + } + if _, err := newServiceNow(connector.Config{Settings: base, Secrets: map[string]string{ + "api.username": "admin", + }}); err == nil { + t.Fatal("expected an error with a username but no password") + } + basic, err := newServiceNow(connector.Config{ Settings: map[string]string{"instance_url": "https://dev12345.service-now.com/", "poll_interval_seconds": "5"}, Secrets: map[string]string{"api.username": "admin", "api.password": "secret"}, }) if err != nil { - t.Fatalf("unexpected error: %v", err) + t.Fatalf("basic: unexpected error: %v", err) } - if s.client.base != "https://dev12345.service-now.com" { - t.Fatalf("base = %q", s.client.base) + if basic.client.base != "https://dev12345.service-now.com" { + t.Fatalf("base = %q", basic.client.base) } - if s.poll != 5*time.Second { - t.Fatalf("poll = %v", s.poll) + if basic.client.auth != basicAuth("admin", "secret") { + t.Fatalf("auth = %q, want basic", basic.client.auth) + } + if basic.poll != 5*time.Second { + t.Fatalf("poll = %v", basic.poll) + } + bearer, err := newServiceNow(connector.Config{ + Settings: base, + Secrets: map[string]string{"api.token": "tok"}, + }) + if err != nil { + t.Fatalf("bearer: unexpected error: %v", err) + } + if bearer.client.auth != "Bearer tok" { + t.Fatalf("auth = %q, want bearer", bearer.client.auth) } }