1
0
mirror of https://github.com/sasjs/adapter.git synced 2025-12-11 01:14:36 +00:00

feat(sas9-support): add support for SAS9 via username/password login

This commit is contained in:
Krishna Acondy
2021-05-27 08:00:15 +01:00
parent f8c6318a88
commit 0b055dd05f
6 changed files with 13912 additions and 996 deletions

14550
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -37,6 +37,7 @@
"license": "ISC",
"devDependencies": {
"@types/jest": "^26.0.22",
"@types/tough-cookie": "^4.0.0",
"cp": "^0.2.0",
"dotenv": "^8.2.0",
"jest": "^26.6.3",
@@ -60,13 +61,12 @@
"main": "index.js",
"dependencies": {
"@sasjs/utils": "^2.10.2",
"assert": "^2.0.0",
"axios": "^0.21.1",
"axios-cookiejar-support": "^1.0.1",
"buffer": "^6.0.3",
"form-data": "^4.0.0",
"https": "^1.0.0",
"node-libcurl": "^2.3.3",
"stream": "0.0.2",
"stream-browserify": "^3.0.0"
"tough-cookie": "^4.0.0",
"url": "^0.11.0"
}
}

View File

@@ -839,8 +839,7 @@ export default class SASjs {
this.sas9JobExecutor = new Sas9JobExecutor(
this.sasjsConfig.serverUrl,
this.sasjsConfig.serverType!,
this.jobsPath,
this.requestClient
this.jobsPath
)
this.computeJobExecutor = new ComputeJobExecutor(

View File

@@ -1,47 +1,29 @@
import { ServerType } from '@sasjs/utils/types'
import { Curl } from 'node-libcurl'
import * as NodeFormData from 'form-data'
import {
ErrorResponse,
JobExecutionError,
LoginRequiredError
} from '../types/errors'
import { generateFileUploadForm } from '../file/generateFileUploadForm'
import { generateTableUploadForm } from '../file/generateTableUploadForm'
import { RequestClient } from '../request/RequestClient'
import { SASViyaApiClient } from '../SASViyaApiClient'
import { isRelativePath } from '../utils'
import { ErrorResponse } from '../types/errors'
import { convertToCSV, isRelativePath } from '../utils'
import { BaseJobExecutor } from './JobExecutor'
import { Sas9RequestClient } from '../request/Sas9RequestClient'
interface WaitingRequstPromise {
promise: Promise<any> | null
resolve: any
reject: any
}
export class Sas9JobExecutor extends BaseJobExecutor {
private requestClient: Sas9RequestClient
constructor(
serverUrl: string,
serverType: ServerType,
private jobsPath: string,
private requestClient: RequestClient
private jobsPath: string
) {
super(serverUrl, serverType)
this.requestClient = new Sas9RequestClient(serverUrl, false)
}
async execute(
sasJob: string,
data: any,
config: any,
loginRequiredCallback?: any
) {
const loginCallback = loginRequiredCallback || (() => Promise.resolve())
async execute(sasJob: string, data: any, config: any) {
const program = isRelativePath(sasJob)
? config.appLoc
? config.appLoc.replace(/\/?$/, '/') + sasJob.replace(/^\//, '')
: sasJob
: sasJob
const jobUri = ''
let apiUrl = `${config.serverUrl}${this.jobsPath}/?${
let apiUrl = `${config.serverUrl}${this.jobsPath}?${
jobUri.length > 0
? '__program=' + program + '&_job=' + jobUri
: '_program=' + program
@@ -56,34 +38,13 @@ export class Sas9JobExecutor extends BaseJobExecutor {
...this.getRequestParams(config)
}
let formData =
typeof FormData === 'undefined' ? new NodeFormData() : new FormData()
let formData = new NodeFormData()
if (data) {
const stringifiedData = JSON.stringify(data)
if (
config.serverType === ServerType.Sas9 ||
stringifiedData.length > 500000 ||
stringifiedData.includes(';')
) {
// file upload approach
try {
formData = generateFileUploadForm(formData as FormData, data)
} catch (e) {
return Promise.reject(new ErrorResponse(e?.message, e))
}
} else {
// param based approach
try {
const {
formData: newFormData,
requestParams: params
} = generateTableUploadForm(formData as FormData, data)
formData = newFormData
requestParams = { ...requestParams, ...params }
} catch (e) {
return Promise.reject(new ErrorResponse(e?.message, e))
}
try {
formData = generateFileUploadForm(formData, data)
} catch (e) {
return Promise.reject(new ErrorResponse(e?.message, e))
}
}
@@ -93,89 +54,21 @@ export class Sas9JobExecutor extends BaseJobExecutor {
}
}
const requestPromise = new Promise((resolve, reject) => {
const curl = new Curl()
curl.setOpt('URL', apiUrl)
curl.setOpt('USERAGENT', 'curl/7.64.1')
curl.setOpt('FOLLOWLOCATION', true)
curl.setOpt('SSL_VERIFYPEER', false)
curl.setOpt('COOKIEFILE', 'cookiefile')
curl.on('end', (statusCode, res, headers) => {
console.log('res', res)
console.log('statusCode', statusCode)
console.log('statusCode', statusCode)
curl.on('end', (statusCode, res, headers) => {
console.log('res', res)
console.log('statusCode', statusCode)
console.log('statusCode', statusCode)
resolve(res)
})
curl.perform()
})
curl.on('error', (error) => {
console.log('error', error)
reject(error)
})
curl.perform()
// this.requestClient!.post(apiUrl, formData, undefined)
// .then(async (res) => {
// if (this.serverType === ServerType.SasViya && config.debug) {
// const jsonResponse = await this.parseSasViyaDebugResponse(
// res.result as string
// )
// this.appendRequest(res, sasJob, config.debug)
// resolve(jsonResponse)
// }
// this.appendRequest(res, sasJob, config.debug)
// resolve(res.result)
// })
// .catch(async (e: Error) => {
// if (e instanceof JobExecutionError) {
// this.appendRequest(e, sasJob, config.debug)
// reject(new ErrorResponse(e?.message, e))
// }
// if (e instanceof LoginRequiredError) {
// await loginCallback()
// this.appendWaitingRequest(() => {
// return this.execute(
// sasJob,
// data,
// config,
// loginRequiredCallback
// ).then(
// (res: any) => {
// resolve(res)
// },
// (err: any) => {
// reject(err)
// }
// )
// })
// } else {
// reject(new ErrorResponse(e?.message, e))
// }
// })
})
return requestPromise
}
private parseSasViyaDebugResponse = async (response: string) => {
const iframeStart = response.split(
'<iframe style="width: 99%; height: 500px" src="'
)[1]
const jsonUrl = iframeStart ? iframeStart.split('"></iframe>')[0] : null
if (!jsonUrl) {
throw new Error('Unable to find webout file URL.')
}
return this.requestClient
.get(this.serverUrl + jsonUrl, undefined)
.then((res) => res.result)
await this.requestClient.login(config.username, config.password)
const contentType =
data && Object.keys(data).length
? 'multipart/form-data; boundary=' + (formData as any)._boundary
: 'text/plain'
return await this.requestClient!.post(
apiUrl,
formData,
undefined,
contentType,
{
Accept: '*/*',
Connection: 'Keep-Alive'
}
)
}
private getRequestParams(config: any): any {
@@ -190,26 +83,26 @@ export class Sas9JobExecutor extends BaseJobExecutor {
return requestParams
}
}
private parseSAS9ErrorResponse(response: string) {
const logLines = response.split('\n')
const parsedLines: string[] = []
let firstErrorLineIndex: number = -1
logLines.map((line: string, index: number) => {
if (
line.toLowerCase().includes('error') &&
!line.toLowerCase().includes('this request completed with errors.') &&
firstErrorLineIndex === -1
) {
firstErrorLineIndex = index
}
})
for (let i = firstErrorLineIndex - 10; i <= firstErrorLineIndex + 10; i++) {
parsedLines.push(logLines[i])
const generateFileUploadForm = (
formData: NodeFormData,
data: any
): NodeFormData => {
for (const tableName in data) {
const name = tableName
const csv = convertToCSV(data[tableName])
if (csv === 'ERROR: LARGE STRING LENGTH') {
throw new Error(
'The max length of a string value in SASjs is 32765 characters.'
)
}
return parsedLines.join(', ')
formData.append(name, csv, {
filename: `${name}.csv`,
contentType: 'application/csv'
})
}
return formData
}

View File

@@ -44,11 +44,11 @@ export interface HttpClient {
}
export class RequestClient implements HttpClient {
private csrfToken: CsrfToken = { headerName: '', value: '' }
private fileUploadCsrfToken: CsrfToken | undefined
private httpClient: AxiosInstance
protected csrfToken: CsrfToken = { headerName: '', value: '' }
protected fileUploadCsrfToken: CsrfToken | undefined
protected httpClient: AxiosInstance
constructor(private baseUrl: string, allowInsecure = false) {
constructor(protected baseUrl: string, allowInsecure = false) {
const https = require('https')
if (allowInsecure && https.Agent) {
this.httpClient = axios.create({
@@ -214,9 +214,8 @@ export class RequestClient implements HttpClient {
const headers = this.getHeaders(accessToken, 'application/json')
if (this.fileUploadCsrfToken?.value) {
headers[
this.fileUploadCsrfToken.headerName
] = this.fileUploadCsrfToken.value
headers[this.fileUploadCsrfToken.headerName] =
this.fileUploadCsrfToken.value
}
try {
@@ -291,7 +290,7 @@ export class RequestClient implements HttpClient {
})
}
private getHeaders = (
protected getHeaders = (
accessToken: string | undefined,
contentType: string
) => {
@@ -316,7 +315,7 @@ export class RequestClient implements HttpClient {
return headers
}
private parseAndSetFileUploadCsrfToken = (response: AxiosResponse) => {
protected parseAndSetFileUploadCsrfToken = (response: AxiosResponse) => {
const token = this.parseCsrfToken(response)
if (token) {
@@ -324,7 +323,7 @@ export class RequestClient implements HttpClient {
}
}
private parseAndSetCsrfToken = (response: AxiosResponse) => {
protected parseAndSetCsrfToken = (response: AxiosResponse) => {
const token = this.parseCsrfToken(response)
if (token) {
@@ -333,9 +332,9 @@ export class RequestClient implements HttpClient {
}
private parseCsrfToken = (response: AxiosResponse): CsrfToken | undefined => {
const tokenHeader = (response.headers[
'x-csrf-header'
] as string)?.toLowerCase()
const tokenHeader = (
response.headers['x-csrf-header'] as string
)?.toLowerCase()
if (tokenHeader) {
const token = response.headers[tokenHeader]
@@ -348,7 +347,7 @@ export class RequestClient implements HttpClient {
}
}
private handleError = async (
protected handleError = async (
e: any,
callback: any,
debug: boolean = false
@@ -406,7 +405,7 @@ export class RequestClient implements HttpClient {
throw e
}
private parseResponse<T>(response: AxiosResponse<any>) {
protected parseResponse<T>(response: AxiosResponse<any>) {
const etag = response?.headers ? response.headers['etag'] : ''
let parsedResponse
let includeSAS9Log: boolean = false
@@ -440,7 +439,7 @@ export class RequestClient implements HttpClient {
}
}
const throwIfError = (response: AxiosResponse) => {
export const throwIfError = (response: AxiosResponse) => {
if (response.status === 401) {
throw new LoginRequiredError()
}

View File

@@ -0,0 +1,117 @@
import { AxiosRequestConfig } from 'axios'
import axiosCookieJarSupport from 'axios-cookiejar-support'
import * as tough from 'tough-cookie'
import { prefixMessage } from '@sasjs/utils/error'
import { RequestClient, throwIfError } from './RequestClient'
export class Sas9RequestClient extends RequestClient {
constructor(baseUrl: string, allowInsecure = false) {
super(baseUrl, allowInsecure)
this.httpClient.defaults.maxRedirects = 0
this.httpClient.defaults.validateStatus = (status) =>
status >= 200 && status < 303
if (axiosCookieJarSupport) {
axiosCookieJarSupport(this.httpClient)
this.httpClient.defaults.jar = new tough.CookieJar()
}
}
public async login(username: string, password: string) {
const codeInjectorPath = `/User Folders/${username}/My Folder/sasjs/runner`
if (this.httpClient.defaults.jar) {
;(this.httpClient.defaults.jar as tough.CookieJar).removeAllCookies()
await this.get(
`/SASStoredProcess/do?_program=${codeInjectorPath}&_username=${username}&_password=${password}`,
undefined,
'text/plain'
)
}
}
public async get<T>(
url: string,
accessToken: string | undefined,
contentType: string = 'application/json',
overrideHeaders: { [key: string]: string | number } = {},
debug: boolean = false
): Promise<{ result: T; etag: string }> {
const headers = {
...this.getHeaders(accessToken, contentType),
...overrideHeaders
}
const requestConfig: AxiosRequestConfig = {
headers,
responseType: contentType === 'text/plain' ? 'text' : 'json',
withCredentials: true
}
if (contentType === 'text/plain') {
requestConfig.transformResponse = undefined
}
return this.httpClient
.get<T>(url, requestConfig)
.then((response) => {
if (response.status === 302) {
return this.get(
response.headers['location'],
accessToken,
contentType
)
}
throwIfError(response)
return this.parseResponse<T>(response)
})
.catch(async (e) => {
return await this.handleError(
e,
() =>
this.get<T>(url, accessToken, contentType, overrideHeaders).catch(
(err) => {
throw prefixMessage(
err,
'Error while executing handle error callback. '
)
}
),
debug
).catch((err) => {
throw prefixMessage(err, 'Error while handling error. ')
})
})
}
public post<T>(
url: string,
data: any,
accessToken: string | undefined,
contentType = 'application/json',
overrideHeaders: { [key: string]: string | number } = {}
): Promise<{ result: T; etag: string }> {
const headers = {
...this.getHeaders(accessToken, contentType),
...overrideHeaders
}
return this.httpClient
.post<T>(url, data, { headers, withCredentials: true })
.then(async (response) => {
if (response.status === 302) {
return await this.get(
response.headers['location'],
undefined,
contentType,
overrideHeaders
)
}
throwIfError(response)
return this.parseResponse<T>(response)
})
.catch(async (e) => {
return await this.handleError(e, () =>
this.post<T>(url, data, accessToken, contentType, overrideHeaders)
)
})
}
}