Files
sam 125457a391
publish / publish (push) Successful in 1m21s
feat(connector): declare privileged effects on every lookup and action (#2071)
Regenerate the connector SDK from the host contract, which adds the
effects_declared, privilege_change and secret_access fields. Every lookup and
action in this repository now states them, and a Semaphore template states
them in its effect settings or is unavailable in chat.
2026-11-21 21:29:49 +11:00

151 lines
4.5 KiB
Go

package main
import (
"context"
"encoding/json/jsontext"
json "encoding/json/v2"
"fmt"
"net/url"
"github.com/samcharles93/archipelago/sdk/connector"
connectorv1 "github.com/samcharles93/archipelago/sdk/connector/v1"
)
// cmdb.go is the CMDB area: the ownership and classification lookups. A target
// resolves through the server CI first, then the application CI, then any CI,
// and reference fields come back as display names so the owner is a person,
// not a sys_id.
const (
ownerLookup = "cmdb_owner"
environmentLookup = "cmdb_environment"
dataClassLookup = "cmdb_data_class"
)
// ciTables is the resolution order for a target name: server, application,
// then any CI.
var ciTables = []string{"cmdb_ci_server", "cmdb_ci_appl", "cmdb_ci"}
var cmdbInputSchema = []byte(`{"type":"object","additionalProperties":false,"required":["target"],"properties":{` +
`"target":{"type":"string","description":"The target to resolve, as a host or application name."}}}`)
func cmdbLookups() []*connectorv1.LookupType {
return []*connectorv1.LookupType{
{
Name: ownerLookup,
Description: "Resolve a target to its CMDB owner, delegate and support group: server CI, then application CI, then any CI.",
InputSchema: cmdbInputSchema,
Effect: connector.NoPrivilegedEffects(),
},
{
Name: environmentLookup,
Description: "The target's environment from the CMDB, for example production.",
InputSchema: cmdbInputSchema,
Effect: connector.NoPrivilegedEffects(),
},
{
Name: dataClassLookup,
Description: "The target's data class from the CMDB.",
InputSchema: cmdbInputSchema,
Effect: connector.NoPrivilegedEffects(),
},
}
}
type cmdbInput struct {
Target string `json:"target"`
}
// findCI returns the first CI matching the target, and which table it came
// from. The whole row is read with display values, because which of the
// candidate fields exist depends on the instance's CMDB schema; the caller
// picks the fields it wants from what is there.
func (s *servicenow) findCI(ctx context.Context, target string) (map[string]any, string, error) {
for _, table := range ciTables {
q := url.Values{
"sysparm_query": {"name=" + target + "^ORhost_name=" + target},
"sysparm_display_value": {"true"},
"sysparm_limit": {"1"},
}
row, err := s.client.one(ctx, table, q)
if err != nil {
return nil, "", err
}
if row != nil {
return row, table, nil
}
}
return nil, "", nil
}
// lookupOwner answers the ownership lookup with the owner chain SPEC section 6
// describes: server, then application, then owner, plus delegate and support
// group.
func (s *servicenow) lookupOwner(ctx context.Context, raw jsontext.Value) (jsontext.Value, bool, error) {
var in cmdbInput
if err := json.Unmarshal(raw, &in, json.RejectUnknownMembers(true)); err != nil {
return nil, false, fmt.Errorf("cmdb_owner: %w", err)
}
if in.Target == "" {
return nil, false, fmt.Errorf("cmdb_owner: target is required")
}
row, table, err := s.findCI(ctx, in.Target)
if err != nil {
return nil, false, err
}
if row == nil {
return nil, false, nil
}
out, err := marshalFields(map[string]any{
"owner": display(row["owner"]),
"delegate": display(row["delegate"]),
"support_group": display(row["support_group"]),
"source": sourceName(table),
})
if err != nil {
return nil, false, err
}
return out, true, nil
}
// lookupCIField answers the environment and data-class lookups: the first
// non-empty of the candidate field names on the target's CI.
func (s *servicenow) lookupCIField(ctx context.Context, raw jsontext.Value, fields ...string) (jsontext.Value, bool, error) {
var in cmdbInput
if err := json.Unmarshal(raw, &in, json.RejectUnknownMembers(true)); err != nil {
return nil, false, err
}
if in.Target == "" {
return nil, false, fmt.Errorf("target is required")
}
row, table, err := s.findCI(ctx, in.Target)
if err != nil {
return nil, false, err
}
if row == nil {
return nil, false, nil
}
for _, f := range fields {
if v := display(row[f]); v != "" {
out, err := marshalFields(map[string]any{"value": v, "source": sourceName(table)})
if err != nil {
return nil, false, err
}
return out, true, nil
}
}
return nil, false, nil
}
// sourceName turns a CI table into the plain word the output schema promises.
func sourceName(table string) string {
switch table {
case "cmdb_ci_server":
return "server"
case "cmdb_ci_appl":
return "application"
default:
return "ci"
}
}