mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-16 08:30:07 +00:00
feat(sas9-support): execute arbitrary code on SAS9 using SASjs runner
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
import axios, { AxiosInstance } from 'axios'
|
import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time'
|
||||||
|
import * as NodeFormData from 'form-data'
|
||||||
|
import { Sas9RequestClient } from './request/Sas9RequestClient'
|
||||||
import { isUrl } from './utils'
|
import { isUrl } from './utils'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -6,11 +8,11 @@ import { isUrl } from './utils'
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export class SAS9ApiClient {
|
export class SAS9ApiClient {
|
||||||
private httpClient: AxiosInstance
|
private requestClient: Sas9RequestClient
|
||||||
|
|
||||||
constructor(private serverUrl: string) {
|
constructor(private serverUrl: string, private jobsPath: string) {
|
||||||
if (serverUrl) isUrl(serverUrl)
|
if (serverUrl) isUrl(serverUrl)
|
||||||
this.httpClient = axios.create({ baseURL: this.serverUrl })
|
this.requestClient = new Sas9RequestClient(serverUrl, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,27 +35,59 @@ export class SAS9ApiClient {
|
|||||||
/**
|
/**
|
||||||
* Executes code on a SAS9 server.
|
* Executes code on a SAS9 server.
|
||||||
* @param linesOfCode - an array of code lines to execute.
|
* @param linesOfCode - an array of code lines to execute.
|
||||||
* @param serverName - the server to execute the code on.
|
* @param userName - the user name to log into the current SAS server.
|
||||||
* @param repositoryName - the repository to execute the code in.
|
* @param password - the password to log into the current SAS server.
|
||||||
*/
|
*/
|
||||||
public async executeScript(
|
public async executeScript(
|
||||||
linesOfCode: string[],
|
linesOfCode: string[],
|
||||||
serverName: string,
|
userName: string,
|
||||||
repositoryName: string
|
password: string
|
||||||
) {
|
) {
|
||||||
const requestPayload = linesOfCode.join('\n')
|
await this.requestClient.login(userName, password, this.jobsPath)
|
||||||
|
|
||||||
const executeScriptResponse = await this.httpClient.put(
|
const formData = generateFileUploadForm(linesOfCode.join('\n'))
|
||||||
`/sas/servers/${serverName}/cmd?repositoryName=${repositoryName}`,
|
|
||||||
`command=${requestPayload}`,
|
const codeInjectorPath = `/User Folders/${userName}/My Folder/sasjs/runner`
|
||||||
{
|
const contentType =
|
||||||
headers: {
|
'multipart/form-data; boundary=' + formData.getBoundary()
|
||||||
Accept: 'application/json'
|
const contentLength = formData.getLengthSync()
|
||||||
},
|
|
||||||
responseType: 'text'
|
const headers = {
|
||||||
}
|
'cache-control': 'no-cache',
|
||||||
|
Accept: '*/*',
|
||||||
|
'Content-Type': contentType,
|
||||||
|
'Content-Length': contentLength,
|
||||||
|
Connection: 'keep-alive'
|
||||||
|
}
|
||||||
|
const storedProcessUrl = `${this.jobsPath}/?${
|
||||||
|
'_program=' + codeInjectorPath + '&_debug=log'
|
||||||
|
}`
|
||||||
|
const response = await this.requestClient.post(
|
||||||
|
storedProcessUrl,
|
||||||
|
formData,
|
||||||
|
undefined,
|
||||||
|
contentType,
|
||||||
|
headers
|
||||||
)
|
)
|
||||||
|
|
||||||
return executeScriptResponse.data
|
return response.result as string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const generateFileUploadForm = (data: any): NodeFormData => {
|
||||||
|
const formData = new NodeFormData()
|
||||||
|
const fileName = `sasjs-execute-sas9-${getTimestamp()}.sas`
|
||||||
|
formData.append(fileName, data, {
|
||||||
|
filename: `${fileName}.csv`,
|
||||||
|
contentType: 'text/plain'
|
||||||
|
})
|
||||||
|
|
||||||
|
return formData
|
||||||
|
}
|
||||||
|
|
||||||
|
const getTimestamp = () => {
|
||||||
|
return timestampToYYYYMMDDHHMMSS()
|
||||||
|
.replace(/:/g, '')
|
||||||
|
.replace(/\//g, '')
|
||||||
|
.replace(/ /g, '')
|
||||||
|
}
|
||||||
|
|||||||
@@ -719,13 +719,11 @@ export class SASViyaApiClient {
|
|||||||
let formData
|
let formData
|
||||||
if (typeof FormData === 'undefined') {
|
if (typeof FormData === 'undefined') {
|
||||||
formData = new NodeFormData()
|
formData = new NodeFormData()
|
||||||
formData.append('grant_type', 'authorization_code')
|
|
||||||
formData.append('code', authCode)
|
|
||||||
} else {
|
} else {
|
||||||
formData = new FormData()
|
formData = new FormData()
|
||||||
formData.append('grant_type', 'authorization_code')
|
|
||||||
formData.append('code', authCode)
|
|
||||||
}
|
}
|
||||||
|
formData.append('grant_type', 'authorization_code')
|
||||||
|
formData.append('code', authCode)
|
||||||
|
|
||||||
const authResponse = await this.requestClient
|
const authResponse = await this.requestClient
|
||||||
.post(
|
.post(
|
||||||
|
|||||||
16
src/SASjs.ts
16
src/SASjs.ts
@@ -59,15 +59,15 @@ export default class SASjs {
|
|||||||
|
|
||||||
public async executeScriptSAS9(
|
public async executeScriptSAS9(
|
||||||
linesOfCode: string[],
|
linesOfCode: string[],
|
||||||
serverName: string,
|
userName: string,
|
||||||
repositoryName: string
|
password: string
|
||||||
) {
|
) {
|
||||||
this.isMethodSupported('executeScriptSAS9', ServerType.Sas9)
|
this.isMethodSupported('executeScriptSAS9', ServerType.Sas9)
|
||||||
|
|
||||||
return await this.sas9ApiClient?.executeScript(
|
return await this.sas9ApiClient?.executeScript(
|
||||||
linesOfCode,
|
linesOfCode,
|
||||||
serverName,
|
userName,
|
||||||
repositoryName
|
password
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -624,7 +624,7 @@ export default class SASjs {
|
|||||||
)
|
)
|
||||||
sasApiClient.debug = this.sasjsConfig.debug
|
sasApiClient.debug = this.sasjsConfig.debug
|
||||||
} else if (this.sasjsConfig.serverType === ServerType.Sas9) {
|
} else if (this.sasjsConfig.serverType === ServerType.Sas9) {
|
||||||
sasApiClient = new SAS9ApiClient(serverUrl)
|
sasApiClient = new SAS9ApiClient(serverUrl, this.jobsPath)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let sasClientConfig: any = null
|
let sasClientConfig: any = null
|
||||||
@@ -813,7 +813,11 @@ export default class SASjs {
|
|||||||
if (this.sasjsConfig.serverType === ServerType.Sas9) {
|
if (this.sasjsConfig.serverType === ServerType.Sas9) {
|
||||||
if (this.sas9ApiClient)
|
if (this.sas9ApiClient)
|
||||||
this.sas9ApiClient!.setConfig(this.sasjsConfig.serverUrl)
|
this.sas9ApiClient!.setConfig(this.sasjsConfig.serverUrl)
|
||||||
else this.sas9ApiClient = new SAS9ApiClient(this.sasjsConfig.serverUrl)
|
else
|
||||||
|
this.sas9ApiClient = new SAS9ApiClient(
|
||||||
|
this.sasjsConfig.serverUrl,
|
||||||
|
this.jobsPath
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.fileUploader = new FileUploader(
|
this.fileUploader = new FileUploader(
|
||||||
|
|||||||
Reference in New Issue
Block a user