1
1
import logger from '../logger' ;
2
+ import { MinioBucketsRegistry , Mediator as OpenHimAPIMediator } from '../types' ;
2
3
import { MediatorConfig } from '../types/mediatorConfig' ;
3
4
import { RequestOptions } from '../types/request' ;
4
5
import { getConfig } from '../config/config' ;
5
6
import axios , { AxiosError } from 'axios' ;
6
7
import https from 'https' ;
7
- import {
8
- activateHeartbeat ,
9
- fetchConfig ,
10
- registerMediator ,
11
- authenticate ,
12
- genAuthHeaders ,
13
- } from 'openhim-mediator-utils' ;
8
+ import { activateHeartbeat , fetchConfig , registerMediator } from 'openhim-mediator-utils' ;
14
9
import { Bucket , createMinioBucketListeners , ensureBucketExists } from '../utils/minioClient' ;
15
10
import path from 'path' ;
16
11
@@ -23,7 +18,7 @@ const resolveMediatorConfig = (): MediatorConfig => {
23
18
let mediatorConfigFile ;
24
19
25
20
try {
26
- logger . info ( `Loading mediator config from: ${ mediatorConfigFilePath } ` ) ;
21
+ logger . debug ( `Loading mediator config from: ${ mediatorConfigFilePath } ` ) ;
27
22
mediatorConfigFile = require ( mediatorConfigFilePath ) ;
28
23
} catch ( error ) {
29
24
logger . error ( `Failed to parse JSON: ${ error } ` ) ;
@@ -43,12 +38,12 @@ const resolveOpenhimConfig = (urn: string): RequestOptions => {
43
38
} ;
44
39
} ;
45
40
46
- export const setupMediator = ( ) => {
41
+ export const setupMediator = async ( ) => {
47
42
try {
48
43
const mediatorConfig = resolveMediatorConfig ( ) ;
49
44
const openhimConfig = resolveOpenhimConfig ( mediatorConfig . urn ) ;
50
45
51
- registerMediator ( openhimConfig , mediatorConfig , ( error : Error ) => {
46
+ await registerMediator ( openhimConfig , mediatorConfig , ( error : Error ) => {
52
47
if ( error ) {
53
48
logger . error ( `Failed to register mediator: ${ JSON . stringify ( error ) } ` ) ;
54
49
throw error ;
@@ -87,8 +82,8 @@ export const setupMediator = () => {
87
82
} ;
88
83
89
84
//TODO: Add Typing and error handling.
90
- async function getMediatorConfig ( ) {
91
- logger . info ( 'Fetching mediator config from OpenHIM' ) ;
85
+ async function getMediatorConfig ( ) : Promise < OpenHimAPIMediator | null > {
86
+ logger . debug ( 'Fetching mediator config from OpenHIM' ) ;
92
87
const mediatorConfig = resolveMediatorConfig ( ) ;
93
88
const openhimConfig = resolveOpenhimConfig ( mediatorConfig . urn ) ;
94
89
@@ -108,17 +103,65 @@ async function getMediatorConfig() {
108
103
return request . data ;
109
104
} catch ( e ) {
110
105
const error = e as AxiosError ;
111
- logger . error ( `Failed to fetch mediator config: ${ JSON . stringify ( error ) } ` ) ;
112
- error . status === 404 && logger . warn ( 'Mediator config not found in OpenHIM, ' ) ;
106
+
107
+ switch ( error . status ) {
108
+ case 401 :
109
+ logger . error ( `Failed to authenticate with OpenHIM, check your credentials` ) ;
110
+ break ;
111
+ case 404 :
112
+ logger . debug (
113
+ 'Mediator config not found in OpenHIM, This is expected on initial setup'
114
+ ) ;
115
+ break ;
116
+ default :
117
+ logger . error ( `Failed to fetch mediator config: ${ JSON . stringify ( error ) } ` ) ;
118
+ break ;
119
+ }
113
120
return null ;
114
121
}
115
122
}
116
123
117
- export async function getRegisterBuckets ( ) : Promise < Bucket [ ] > {
124
+ async function putMediatorConfig ( mediatorUrn : string , mediatorConfig : MinioBucketsRegistry [ ] ) {
125
+ const openhimConfig = resolveOpenhimConfig ( mediatorUrn ) ;
126
+ const { apiURL, username, password, trustSelfSigned } = openhimConfig ;
127
+ await axios . put (
128
+ `${ apiURL } /mediators/urn:mediator:climate-mediator/config` ,
129
+ {
130
+ minio_buckets_registry : mediatorConfig ,
131
+ } ,
132
+ {
133
+ auth : { username, password } ,
134
+ httpsAgent : new https . Agent ( {
135
+ rejectUnauthorized : ! trustSelfSigned ,
136
+ } ) ,
137
+ }
138
+ ) ;
139
+
140
+ try {
141
+ logger . info ( 'Successfully updated mediator config in OpenHIM' ) ;
142
+ } catch ( error ) {
143
+ const axiosError = error as AxiosError ;
144
+ switch ( axiosError . status ) {
145
+ case 401 :
146
+ logger . error ( `Failed to authenticate with OpenHIM, check your credentials` ) ;
147
+ break ;
148
+ default :
149
+ logger . error (
150
+ `Failed to update mediator config in OpenHIM: ${ JSON . stringify ( axiosError ) } `
151
+ ) ;
152
+ break ;
153
+ }
154
+ }
155
+ }
156
+
157
+ export async function getRegisteredBuckets ( ) : Promise < Bucket [ ] > {
118
158
if ( runningMode !== 'testing' ) {
119
159
logger . info ( 'Fetching registered buckets from OpenHIM' ) ;
120
160
const mediatorConfig = await getMediatorConfig ( ) ;
121
161
162
+ if ( ! mediatorConfig ) {
163
+ return [ ] ;
164
+ }
122
165
//TODO: Handle errors, and undefined response.
123
166
const buckets = mediatorConfig . config ?. minio_buckets_registry as Bucket [ ] ;
124
167
if ( ! buckets ) {
@@ -133,13 +176,49 @@ export async function getRegisterBuckets(): Promise<Bucket[]> {
133
176
}
134
177
135
178
export async function registerBucket ( bucket : string , region ?: string ) {
136
- if ( runningMode !== 'testing' ) {
179
+ // If we are in testing mode, we don't need to have the registered buckets persisted
180
+ if ( runningMode === 'testing' ) {
181
+ logger . debug ( 'Running in testing mode, skipping bucket registration' ) ;
137
182
return true ;
138
183
}
184
+
185
+ //get the mediator config from OpenHIM
139
186
const mediatorConfig = await getMediatorConfig ( ) ;
140
- const existingBuckets = mediatorConfig . config ?. minio_buckets_registry ;
141
187
142
- if ( ! existingBuckets ) {
143
- return [ ] ;
188
+ //TODO: Change this to a debug log
189
+ logger . debug ( `Mediator config: ${ JSON . stringify ( mediatorConfig ) } ` ) ;
190
+
191
+ //if the mediator config is not found, log the issue and return false
192
+ if ( mediatorConfig === null ) {
193
+ logger . error ( 'Mediator config not found in OpenHIM, unable to register bucket' ) ;
194
+ return false ;
144
195
}
196
+
197
+ const newBucket = {
198
+ bucket,
199
+ region : region || '' ,
200
+ } ;
201
+
202
+ //get the existing buckets from the mediator config
203
+ const existingConfig = mediatorConfig . config ;
204
+
205
+ if ( existingConfig === undefined ) {
206
+ logger . info ( 'Mediator config does not have a config section, creating new config' ) ;
207
+ mediatorConfig [ 'config' ] = {
208
+ minio_buckets_registry : [ newBucket ] ,
209
+ } ;
210
+ } else {
211
+ const existingBucket = existingConfig . minio_buckets_registry . find (
212
+ ( bucket ) => bucket . bucket === newBucket . bucket
213
+ ) ;
214
+ if ( existingBucket ) {
215
+ logger . debug ( `Bucket ${ bucket } already exists in the config` ) ;
216
+ return false ;
217
+ }
218
+ logger . info ( `Adding bucket ${ bucket } to OpenHIM config` ) ;
219
+ existingConfig . minio_buckets_registry . push ( newBucket ) ;
220
+ await putMediatorConfig ( mediatorConfig . urn , existingConfig . minio_buckets_registry ) ;
221
+ }
222
+
223
+ return true ;
145
224
}
0 commit comments