Skip to content

Commit 19f3641

Browse files
committed
feat: yaml format, new public methods for credentials
1 parent 0ea6a68 commit 19f3641

File tree

2 files changed

+69
-23
lines changed

2 files changed

+69
-23
lines changed

adonis-typings/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
declare module '@ioc:Adonis/Addons/Credentials' {
22
export interface CredentialsContract {
3-
get(key?: string): string | Record<string, string>
3+
get(key?: string): string | boolean | number | Record<string, string | boolean | number>
4+
key(): string | null
5+
content(): string
6+
format(): string
47
initialize(): void
58
}
69

src/Credentials/index.ts

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import fs from 'fs'
1111
import dotenv from 'dotenv'
12+
import YAML from 'yaml'
1213
import { join } from 'path'
1314
import { Exception } from '@poppinss/utils'
1415
import { string } from '@poppinss/utils/build/helpers'
@@ -18,11 +19,12 @@ import { Vault } from '../Vault'
1819
export class Credentials implements CredentialsContract {
1920
private initialized = false
2021
private env = 'development'
21-
private key: string | null = null
22+
private keyValue: string | null = null
2223
private keyParam = 'APP_CREDENTIALS_KEY'
24+
private credentialsFormat: 'yaml' | 'json' = 'yaml'
2325
private credentialsPath = join('resources', 'credentials')
24-
private content: string = ''
25-
private credentials: Record<string, string> = {}
26+
private credentialsContent: string = ''
27+
private credentialsValues: Record<string, string | boolean | number> = {}
2628

2729
constructor(args?: {
2830
env?: string
@@ -35,12 +37,12 @@ export class Credentials implements CredentialsContract {
3537
}
3638

3739
this.env = args?.env || process.env.NODE_ENV || this.env
38-
this.key = args?.key || process.env[this.keyParam] || this.key
40+
this.keyValue = args?.key || process.env[this.keyParam] || this.keyValue
3941
this.credentialsPath = args?.credentialsPath || this.credentialsPath
4042
}
4143

4244
private check() {
43-
if (!this.key && !fs.existsSync(`${this.credentialsPath}/${this.env}.key`)) {
45+
if (!this.keyValue && !fs.existsSync(`${this.credentialsPath}/${this.env}.key`)) {
4446
throw new Exception(
4547
`Credentials key for '${this.env}' environment does not exist, please set it in a file or in ${this.keyParam} environment variable`,
4648
500,
@@ -58,52 +60,93 @@ export class Credentials implements CredentialsContract {
5860
}
5961

6062
private read() {
61-
const key = this.key || fs.readFileSync(`${this.credentialsPath}/${this.env}.key`)
63+
const key = this.keyValue || fs.readFileSync(`${this.credentialsPath}/${this.env}.key`)
6264
const content = fs.readFileSync(`${this.credentialsPath}/${this.env}.credentials`)
6365
const decryptedContent = Vault.decrypt(content.toString('utf-8'), key.toString('utf-8'))
64-
this.content = decryptedContent
66+
67+
this.keyValue = key.toString('utf-8')
68+
this.credentialsContent = decryptedContent
6569
}
6670

6771
private validate() {
6872
try {
69-
JSON.parse(this.content)
70-
} catch (error) {
71-
throw new Exception(
72-
`Credentials file for '${this.env}' environment is corrupted, should be a valid JSON`,
73-
500,
74-
'E_CREDENTIALS_WRONG_FORMAT'
75-
)
73+
JSON.parse(this.credentialsContent)
74+
this.credentialsFormat = 'json'
75+
} catch {
76+
try {
77+
YAML.parse(this.credentialsContent)
78+
this.credentialsFormat = 'yaml'
79+
} catch {
80+
throw new Exception(
81+
`Credentials file for '${this.env}' environment is corrupted, should be a valid YAML or JSON`,
82+
500,
83+
'E_CREDENTIALS_WRONG_FORMAT'
84+
)
85+
}
7686
}
7787
}
7888

79-
private parse(obj?: Object, current?: string) {
80-
const object = obj || JSON.parse(this.content)
89+
private parse(obj?: Record<string, any>, current?: string) {
90+
let object = obj
91+
92+
if (!object && this.credentialsFormat === 'json') {
93+
object = JSON.parse(this.credentialsContent)
94+
}
95+
96+
if (!object && this.credentialsFormat === 'yaml') {
97+
object = YAML.parse(this.credentialsContent)
98+
}
99+
81100
for (const key in object) {
82101
const value = object[key]
83102
const newKey = current ? current + '_' + key : key
84103
if (value && typeof value === 'object') {
85104
this.parse(value, newKey)
86105
} else {
87-
this.credentials[string.snakeCase(newKey).toUpperCase()] = value
106+
this.credentialsValues[string.snakeCase(newKey).toUpperCase()] = value
88107
}
89108
}
90109
}
91110

92111
private populate() {
93-
for (let key in this.credentials) {
94-
process.env[key] = this.credentials[key]
112+
for (let key in this.credentialsValues) {
113+
process.env[key] = `${this.credentialsValues[key]}`
114+
}
115+
}
116+
117+
public key(): string | null {
118+
if (!this.initialized) {
119+
this.initialize()
120+
}
121+
122+
return this.keyValue
123+
}
124+
125+
public content(): string {
126+
if (!this.initialized) {
127+
this.initialize()
95128
}
129+
130+
return this.credentialsContent
131+
}
132+
133+
public format(): string {
134+
if (!this.initialized) {
135+
this.initialize()
136+
}
137+
138+
return this.credentialsFormat
96139
}
97140

98-
public get(key?: string): string | Record<string, string> {
141+
public get(key?: string): string | boolean | number | Record<string, string | boolean | number> {
99142
if (!this.initialized) {
100143
this.initialize()
101144
}
102145

103146
if (key) {
104-
return this.credentials[key]
147+
return this.credentialsValues[key]
105148
} else {
106-
return this.credentials
149+
return this.credentialsValues
107150
}
108151
}
109152

0 commit comments

Comments
 (0)