9
9
10
10
import fs from 'fs'
11
11
import dotenv from 'dotenv'
12
+ import YAML from 'yaml'
12
13
import { join } from 'path'
13
14
import { Exception } from '@poppinss/utils'
14
15
import { string } from '@poppinss/utils/build/helpers'
@@ -18,11 +19,12 @@ import { Vault } from '../Vault'
18
19
export class Credentials implements CredentialsContract {
19
20
private initialized = false
20
21
private env = 'development'
21
- private key : string | null = null
22
+ private keyValue : string | null = null
22
23
private keyParam = 'APP_CREDENTIALS_KEY'
24
+ private credentialsFormat : 'yaml' | 'json' = 'yaml'
23
25
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 > = { }
26
28
27
29
constructor ( args ?: {
28
30
env ?: string
@@ -35,12 +37,12 @@ export class Credentials implements CredentialsContract {
35
37
}
36
38
37
39
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
39
41
this . credentialsPath = args ?. credentialsPath || this . credentialsPath
40
42
}
41
43
42
44
private check ( ) {
43
- if ( ! this . key && ! fs . existsSync ( `${ this . credentialsPath } /${ this . env } .key` ) ) {
45
+ if ( ! this . keyValue && ! fs . existsSync ( `${ this . credentialsPath } /${ this . env } .key` ) ) {
44
46
throw new Exception (
45
47
`Credentials key for '${ this . env } ' environment does not exist, please set it in a file or in ${ this . keyParam } environment variable` ,
46
48
500 ,
@@ -58,52 +60,93 @@ export class Credentials implements CredentialsContract {
58
60
}
59
61
60
62
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` )
62
64
const content = fs . readFileSync ( `${ this . credentialsPath } /${ this . env } .credentials` )
63
65
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
65
69
}
66
70
67
71
private validate ( ) {
68
72
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
+ }
76
86
}
77
87
}
78
88
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
+
81
100
for ( const key in object ) {
82
101
const value = object [ key ]
83
102
const newKey = current ? current + '_' + key : key
84
103
if ( value && typeof value === 'object' ) {
85
104
this . parse ( value , newKey )
86
105
} else {
87
- this . credentials [ string . snakeCase ( newKey ) . toUpperCase ( ) ] = value
106
+ this . credentialsValues [ string . snakeCase ( newKey ) . toUpperCase ( ) ] = value
88
107
}
89
108
}
90
109
}
91
110
92
111
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 ( )
95
128
}
129
+
130
+ return this . credentialsContent
131
+ }
132
+
133
+ public format ( ) : string {
134
+ if ( ! this . initialized ) {
135
+ this . initialize ( )
136
+ }
137
+
138
+ return this . credentialsFormat
96
139
}
97
140
98
- public get ( key ?: string ) : string | Record < string , string > {
141
+ public get ( key ?: string ) : string | boolean | number | Record < string , string | boolean | number > {
99
142
if ( ! this . initialized ) {
100
143
this . initialize ( )
101
144
}
102
145
103
146
if ( key ) {
104
- return this . credentials [ key ]
147
+ return this . credentialsValues [ key ]
105
148
} else {
106
- return this . credentials
149
+ return this . credentialsValues
107
150
}
108
151
}
109
152
0 commit comments