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.
This commit is contained in:
5 files changed
+65
-20
No files matched your search
@@ -23,9 +23,11 @@ Settings (see `package.yaml`):
|
||||
- `instance_url` (required): `https://<instance>.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
|
||||
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user