Repository navigation
[Bug / Question] now-sdk install fails with 401 Unauthorized in headless CI/CD using OAuth client_credentials grant #133
Replies: 3 comments 13 replies
|
Just wanted to let you know that we're looking at this. Thanks for the incredible detail and level of research. |
|
We have changed this auth dance flow for oauth in the 4.12 release that is coming tomorrow (9/10) and removes the hop through angular.do which was mainly done for a CSRF token required by the install process and was inadvertently changing your auth type to basic. It should now stay as oauth and not use angular.do any longer, so if you could try this with the new version when it is available. |
|
Thanks @bryce-godfrey and @thijsdaemen! We upgraded to 1. What's Working (Interactive Developer Workflow)
2. Headless CI/CD & The Enterprise Code Custody RequirementFor enterprises with strict Code Custody & Data Sovereignty requirements, publishing source code to the external ServiceNow Application Repository ( In testing headless CI/CD (
Question for the SDK Team:Given that enterprise code sovereignty policies mandate direct on-instance deployment (
Thanks again for the rapid fix in 4.12.2! |
Uh oh!
There was an error while loading. Please reload this page.
Description & Context
We are running automated headless CI/CD deployments using
@servicenow/sdk(v4.11.0) in GitHub Actions. Due to enterprise source code custody and sovereignty policies, our pipelines must deploy application packages directly to our dedicated ServiceNow instances usingnow-sdk install, rather than publishing proprietary source code to the hosted external Application Repository (now-sdk cicd publish).While
now-sdk installworks reliably with Basic Authentication, attempting to authenticate with OAuth 2.0 Client Credentials (SN_SDK_AUTH_TYPE: oauth) fails with a401 Unauthorizedduring the pre-installation status check against the Table API.We confirmed via instance logs (
Source=OAUTH_SERVER) that the access token is successfully granted to the service account. Through source code tracing of@servicenow/sdk-cliand@servicenow/sdk-api, we identified that the SDK converts the OAuth session to abasiccookie session, which drops theAuthorization: Bearerheader on subsequent REST Table API calls.Environment
@servicenow/sdk/@servicenow/sdk-cliv4.11.0SN_SDK_NODE_ENV: SN_SDK_CI_INSTALLSN_SDK_AUTH_TYPE: oauthSN_SDK_INSTANCE_URL:https://<subprod>.service-now.comSN_SDK_CLIENT_ID:<client_id>SN_SDK_CLIENT_SECRET:<client_secret>Instance-Side Prerequisites Verified
We ensured all documented prerequisites for inbound Client Credentials were met:
glide.oauth.inbound.client.credential.grant_type.enabled=true.oauth_entity):Public Client=falseDefault Grant type=client_credentialsInbound Grant type=client_credentialScope Restriction Status=useraccountmapped to the designated service user.sys_user):Identity Type=Human(identity_type: human)adminrole.Observed Behavior & Error
During the pipeline run, the SDK successfully requests and receives an OAuth access token from
/oauth_token.do. The first verification request against/api/now/table/sys_scopesucceeds (HTTP 200), but the immediate next call to check application upgrade status fails:Instance Logs Verification
OAUTH_SERVER Log:
OAuth token(s) of access_token is/are granted to app:GitHub Actions CI/CD;grant_type:client_credentials;user:svc_xxx_yyy
syslog_transaction:
URL: /api/now/table/sys_scope?sysparm_fields=sys_id,sys_class_name&sysparm_limit=1 | Response Time: 96ms | Status: 200 OK
Immediately after, the call to /api/now/table/sys_upgrade_history is rejected with 401 Unauthorized.
Root Cause Analysis in SDK Source
Decompiling the CLI and API auth modules reveals why this happens:
getUserSession() attempts to establish a browser/cookie session by calling getCSRFToken(). Upon completion, it returns an object with type: 'basic'.
Dropped Authorization Header in credentials.js:
Because the session type was converted to 'basic', LazyCredential.getHeaders() only attaches X-UserToken and Cookie. The Authorization: Bearer <access_token> header is completely removed.
For OAuth Client Credentials machine tokens, ServiceNow's REST Table API does not maintain authentication solely via cookies in cookieJar. It requires an explicit Authorization header (Authorization: Bearer ). When Connector.
appUpgradeStatus sends the request without the Authorization header, the platform rejects it with 401 Unauthorized.
(Note: We also tested enabling bind_token_to_session: true on the oauth_entity record, but the Table API still strictly enforces the presence of the Authorization header).
Questions & Next Steps
We appreciate any insights or guidance from the SDK team!
All reactions