Skip to content

Commit

Permalink
Merge pull request #130 from sasjs/issue-555
Browse files Browse the repository at this point in the history
feat: replaced allowInsecureRequests with httpsAgentOptions
  • Loading branch information
YuryShkoda authored Nov 1, 2021
2 parents 178dc9c + 3267b6e commit fdf43bc
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/types/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {
} from './config'
import { ServerType } from './serverType'
import { TargetJson } from './target'
import { HttpsAgentOptions } from './httpsAgentOptions'

export interface Configuration {
$schema?: string
allowInsecureRequests?: boolean
httpsAgentOptions?: HttpsAgentOptions
docConfig?: DocConfig
buildConfig?: BuildConfig
deployConfig?: DeployConfig
Expand Down
8 changes: 8 additions & 0 deletions src/types/httpsAgentOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as https from 'https'

export interface HttpsAgentOptions extends https.AgentOptions {
caPath?: string
keyPath?: string
certPath?: string
allowInsecureRequests: boolean
}
11 changes: 6 additions & 5 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export * from './config'
export * from './configuration'
export * from './decodedToken'
export * from './extraResponseAttributes'
export * from './httpsAgentOptions'
export * from './macro'
export * from './sasAuthResponse'
export * from './serverType'
export * from './serverError'
export * from './serverType'
export * from './target'
export * from './config'
export * from './macro'
export * from './extraResponseAttributes'
export * from './decodedToken'
17 changes: 9 additions & 8 deletions src/types/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import {
TestConfig
} from './config'
import { ServerType } from './serverType'
import { HttpsAgentOptions } from './httpsAgentOptions'
import {
validateTargetName,
validateServerUrl,
validateServerType,
validateDocConfig,
validateAllowInsecureRequests,
validateHttpsAgentOptions,
validateAppLoc,
validateBuildConfig,
validateDeployConfig,
Expand All @@ -34,7 +35,7 @@ export interface TargetJson {
name: string
serverUrl: string
serverType: ServerType
allowInsecureRequests: boolean
httpsAgentOptions?: HttpsAgentOptions
contextName?: string
serverName?: string
repositoryName?: string
Expand Down Expand Up @@ -69,10 +70,10 @@ export class Target implements TargetJson {
}
private _serverType = ServerType.SasViya

get allowInsecureRequests(): boolean {
return this._allowInsecureRequests
get httpsAgentOptions(): HttpsAgentOptions | undefined {
return this._httpsAgentOptions
}
private _allowInsecureRequests
private _httpsAgentOptions

get appLoc(): string {
return this._appLoc
Expand Down Expand Up @@ -158,8 +159,8 @@ export class Target implements TargetJson {
this._name = validateTargetName(json.name)
this._serverUrl = validateServerUrl(json.serverUrl)
this._serverType = validateServerType(json.serverType)
this._allowInsecureRequests = validateAllowInsecureRequests(
json.allowInsecureRequests
this._httpsAgentOptions = validateHttpsAgentOptions(
json.httpsAgentOptions
)
this._appLoc = validateAppLoc(json.appLoc)
this._contextName = validateContextName(
Expand Down Expand Up @@ -225,7 +226,7 @@ export class Target implements TargetJson {
name: this.name,
serverUrl: this.serverUrl,
serverType: this.serverType,
allowInsecureRequests: this.allowInsecureRequests,
httpsAgentOptions: this.httpsAgentOptions,
appLoc: this.appLoc,
macroFolders: this.macroFolders,
programFolders: this.programFolders,
Expand Down
54 changes: 35 additions & 19 deletions src/types/targetValidators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import {
TestConfig
} from './config'
import { ServerType } from './serverType'
import { HttpsAgentOptions } from './httpsAgentOptions'
import {
validateTargetName,
validateServerType,
validateAllowInsecureRequests,
validateHttpsAgentOptions,
validateAppLoc,
validateServerUrl,
validateBuildConfig,
Expand Down Expand Up @@ -156,38 +157,53 @@ describe('validateServerUrl', () => {
})
})

describe('validateAllowInsecureRequests', () => {
it('should set allowInsecureRequests to false when it is null', () => {
expect(validateAllowInsecureRequests(null as unknown as boolean)).toEqual(
false
)
describe('validateHttpsAgentOptions', () => {
it('should set httpsAgentOptions to undefined when it is null', () => {
expect(
validateHttpsAgentOptions(null as unknown as HttpsAgentOptions)
).toBeUndefined()
})

it('should set httpsAgentOptions to undefined when it is undefined', () => {
expect(
validateHttpsAgentOptions(undefined as unknown as HttpsAgentOptions)
).toBeUndefined()
})

it('should set allowInsecureRequests to false when it is undefined', () => {
it('should remove invalid property types of httpsAgentOptions', () => {
expect(
validateAllowInsecureRequests(undefined as unknown as boolean)
).toEqual(false)
validateHttpsAgentOptions({
caPath: true,
keyPath: 123,
certPath: {}
} as unknown as HttpsAgentOptions)
).toEqual({ allowInsecureRequests: false })
})

it('should throw an error when allowInsecureRequests is not a boolean', () => {
it('should throw an error when httpsAgentOptions is not an object', () => {
expect(() =>
validateAllowInsecureRequests('some-string' as unknown as boolean)
validateHttpsAgentOptions('some-string' as unknown as HttpsAgentOptions)
).toThrowError(
'Invalid value: `allowInsecureRequests` should either be an empty or a boolean'
'Invalid value: `httpsAgentOptions` should either be an empty or an object of `HttpsAgentOptions`'
)
expect(() =>
validateAllowInsecureRequests({} as unknown as boolean)
validateHttpsAgentOptions(true as unknown as HttpsAgentOptions)
).toThrowError(
'Invalid value: `allowInsecureRequests` should either be an empty or a boolean'
'Invalid value: `httpsAgentOptions` should either be an empty or an object of `HttpsAgentOptions`'
)
})

it('should return allowInsecureRequests when false', () => {
expect(validateAllowInsecureRequests(false)).toEqual(false)
})
it('should return httpsAgentOptions when valid', () => {
const allowInsecureRequestsOptions = { allowInsecureRequests: true }
expect(validateHttpsAgentOptions(allowInsecureRequestsOptions)).toEqual(
allowInsecureRequestsOptions
)

it('should return allowInsecureRequests when true', () => {
expect(validateAllowInsecureRequests(true)).toEqual(true)
const caPathOptions = {
allowInsecureRequests: false,
caPath: 'path/to/ca/file'
}
expect(validateHttpsAgentOptions(caPathOptions)).toEqual(caPathOptions)
})
})

Expand Down
41 changes: 32 additions & 9 deletions src/types/targetValidators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import validUrl from 'valid-url'
import { ServerType } from '.'
import { ServerType, HttpsAgentOptions } from '.'
import {
DocConfig,
AuthConfig,
Expand Down Expand Up @@ -76,18 +76,41 @@ export const validateServerUrl = (serverUrl: string): string => {
return serverUrl
}

export const validateAllowInsecureRequests = (
allowInsecureRequests: boolean
): boolean => {
if (allowInsecureRequests === null || allowInsecureRequests === undefined) {
allowInsecureRequests = false
} else if (typeof allowInsecureRequests !== 'boolean') {
export const validateHttpsAgentOptions = (
httpsAgentOptions: HttpsAgentOptions
): HttpsAgentOptions | undefined => {
if (!httpsAgentOptions) return

if (typeof httpsAgentOptions !== 'object') {
throw new Error(
'Invalid value: `httpsAgentOptions` should either be an empty or an object of `HttpsAgentOptions`'
)
}

if (
httpsAgentOptions.allowInsecureRequests === null ||
httpsAgentOptions.allowInsecureRequests === undefined
) {
httpsAgentOptions.allowInsecureRequests = false
} else if (typeof httpsAgentOptions.allowInsecureRequests !== 'boolean') {
throw new Error(
'Invalid value: `allowInsecureRequests` should either be an empty or a boolean'
'Invalid value: `httpsAgentOptions.allowInsecureRequests` should either be an empty or a boolean'
)
}

return allowInsecureRequests
if (typeof httpsAgentOptions.caPath !== 'string') {
httpsAgentOptions.caPath = undefined
}

if (typeof httpsAgentOptions.keyPath !== 'string') {
httpsAgentOptions.keyPath = undefined
}

if (typeof httpsAgentOptions.certPath !== 'string') {
httpsAgentOptions.certPath = undefined
}

return httpsAgentOptions
}

export const validateAppLoc = (appLoc: string): string => {
Expand Down

0 comments on commit fdf43bc

Please sign in to comment.