Search code examples
node.jsoracle-cloud-infrastructure

Create a Bastion port forwarding session with Node API


I'm trying to create a port forwarding session using this sample but getting an error message that states, "errorObject: OciError: Missing Parameter."

I've made an attempt to use the same parameters via the command-line interface, and it creates the port forwarding session successfully, but when I attempt to achieve the same functionality using Node.js APIs, I encounter this issue.

Below is my code snippet and the response that I've received. I would greatly appreciate it if someone could help me identify what I might be doing incorrectly or if there's a potential issue with the TypeScript SDK for Oracle Cloud Infrastructure. Code

const provider = new common.ConfigFileAuthenticationDetailsProvider();

const bastionClient = new bastion.BastionClient({ authenticationDetailsProvider: provider });

// Create the target resource details object

const fileContent = fs.readFileSync(filePath, 'utf8');

const targetResourceDetails = JSON.parse(fileContent);

// Create a request and dependent object(s).

const createSessionDetails = {

displayName: "Session-" + user.toLowerCase() + "-" + portStart + "-" + new Date().toISOString().slice(0, 16).replace(/[-:T]/g, ""),

bastionId: BASTIONID,

targetResourceDetails: targetResourceDetails,

keyType: bastion.models.CreateSessionDetails.KeyType.Pub,

keyDetails: {

publicKeyContent: "ssh-rsa …"

},

sessionTtlInSeconds: 10800

};

console.log("req: ", createSessionDetails);

// create the session

const createSessionRequest = bastion.requests.CreateSessionRequest = {

createSessionDetails: JSON.stringify(createSessionDetails)

};

const createSessionResponse = await bastionClient.createSession(createSessionRequest);

Error

error from defaultErrorFunction: {
response: Response {
[Symbol(realm)]: null,
[Symbol(state)]: {
aborted: false,
rangeRequested: false,
timingAllowPassed: true,
requestIncludesCredentials: true,
type: 'default',
status: 400,
timingInfo: [Object],
cacheState: '',
statusText: 'Bad Request',
headersList: [HeadersList],
urlList: [Array],
body: [Object]
},
[Symbol(headers)]: HeadersList {
cookies: null,
[Symbol(headers map)]: [Map],
[Symbol(headers map sorted)]: null
}
},
errorObject: OciError: Missing Parameter
at Object.handleErrorResponse (/tmp/pfw-oci/node_modules/oci-common/lib/helper.js:34:16)
at Function.<anonymous> (/tmp/pfw-oci/node_modules/oci-common/lib/circuit-breaker.js:30:50)
at Generator.next (<anonymous>)
at fulfilled (/tmp/pfw-oci/node_modules/oci-common/lib/circuit-breaker.js:9:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
statusCode: 400,
serviceCode: 'MissingParameter',
opcRequestId: '…',
targetService: 'Bastion',
operationName: 'createSession',
timestamp: '2023-10-01T22:33:48.055Z',
requestEndpoint: 'POST https://bastion.ap-singapore-1.oci.oraclecloud.com/20210331/sessions',
clientVersion: 'Oracle-TypeScriptSDK/2.70.1',
loggingTips: 'To get more info on the failing request, refer to https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/typescriptsdkconcepts.htm#typescriptsdkconcepts_topic_Logging for ways to log the request/response details.',
troubleshootingTips: "See https://docs.oracle.com/iaas/Content/API/References/apierrors.htm#apierrors_400__400_missingparameter for more information about resolving this error Also see https://docs.oracle.com/iaas/api/#/en/bastion/20210331/Session/CreateSession for details on this operation's requirements. If you are unable to resolve this Bastion issue, please contact Oracle support and provide them this full error message."
}
}

Solution

  • Looks like you shouldn't stringify the createSessionDetails in the payload

        const createSessionRequest = bastion.requests.CreateSessionRequest = {
             createSessionDetails: JSON.stringify(createSessionDetails)
        };
    

    From the Typescript example - the createSessionDetails should just be a plain object

    Modify to

        const createSessionRequest = bastion.requests.CreateSessionRequest = { createSessionDetails };
    

    https://docs.oracle.com/en-us/iaas/api/#/en/bastion/20210331/Session/CreateSession