config.ts 9.28 KB
Newer Older
Diego Molteni's avatar
Diego Molteni committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// ============================================================================
// Copyright 2017-2019, Schlumberger
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ============================================================================

import { CloudFactory } from './cloud';

export interface IConfig {
    init(): Promise<void>;
}

export interface ConfigModel {
    SERVICE_ENV: string;
    SERVICE_PORT: number;
    IMP_SERVICE_ACCOUNT_SIGNER: string;
    LOCKSMAP_REDIS_INSTANCE_ADDRESS: string;
    LOCKSMAP_REDIS_INSTANCE_PORT: number;
    LOCKSMAP_REDIS_INSTANCE_KEY?: string;
    DES_REDIS_INSTANCE_ADDRESS: string;
    DES_REDIS_INSTANCE_PORT: number;
    DES_REDIS_INSTANCE_KEY?: string;
33
34
35
36
    DES_SERVICE_HOST_ENTITLEMENT: string;
    DES_SERVICE_HOST_COMPLIANCE: string;
    DES_SERVICE_HOST_STORAGE: string;
    DES_SERVICE_HOST_PARTITION: string;
Diego Molteni's avatar
Diego Molteni committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    DES_SERVICE_APPKEY: string;
    DES_GROUP_CHAR_LIMIT: number;
    JWKS_URL: string;
    JWT_EXCLUDE_PATHS: string;
    JWT_AUDIENCE: string;
    JWT_ENABLE_FEATURE: boolean;
    API_BASE_PATH: string;
    TENANT_JOURNAL_ON_DATA_PARTITION: boolean;
    FEATURE_FLAG_AUTHORIZATION: boolean;
    FEATURE_FLAG_LEGALTAG: boolean;
    FEATURE_FLAG_SEISMICMETA_STORAGE: boolean;
    FEATURE_FLAG_IMPTOKEN: boolean;
    FEATURE_FLAG_STORAGE_CREDENTIALS: boolean;
    FEATURE_FLAG_TRACE: boolean;
    FEATURE_FLAG_LOGGING: boolean;
    FEATURE_FLAG_STACKDRIVER_EXPORTER: boolean;
}

export abstract class Config implements IConfig {

    // Unit Test activation flag
    public static UTEST: string;

60
61
62
    // Cache TLS DISABLE flag
    public static CACHE_TLS_DISABLE: string;

Diego Molteni's avatar
Diego Molteni committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    // Service base configurations
    public static SERVICE_ENV: string;
    public static SERVICE_PORT: number;
    public static CLOUDPROVIDER: string;

    // API base path
    public static API_BASE_PATH: string;

    // Seismic Store path prefix
    public static SDPATHPREFIX = 'sd://';

    // Namespace and Kind catalog definitions
    public static ORGANIZATION_NS = 'organization';
    public static SEISMIC_STORE_NS = 'seismic-store';
    public static TENANTS_KIND = 'tenants';
    public static SUBPROJECTS_KIND = 'subprojects';
    public static DATASETS_KIND = 'datasets';
    public static SEISMICMETA_KIND = 'seismicmeta';
    public static APPS_KIND = 'apps';

    // Listing modes
    public static LS_MODE = { ALL: 'all', DATASETS: 'datasets', DIRS: 'dirs' };

    // Impersonation Token Service Account [this is the account used to sign the impersonation token]
    public static IMP_SERVICE_ACCOUNT_SIGNER: string;

    // Consistency Data Object
    public static FILE_CDO = 'dataconsistency.cdo';

    // Redis cache for lock
    public static LOCKSMAP_REDIS_INSTANCE_ADDRESS: string;
    public static LOCKSMAP_REDIS_INSTANCE_PORT: number;
    public static LOCKSMAP_REDIS_INSTANCE_KEY: string;

    // Redis cache for DataEcosystem results
    public static DES_REDIS_INSTANCE_ADDRESS: string;
    public static DES_REDIS_INSTANCE_PORT: number;
    public static DES_REDIS_INSTANCE_KEY: string;

    // DataEcosystem Configuration
103
104
105
106
    public static DES_SERVICE_HOST_ENTITLEMENT: string;
    public static DES_SERVICE_HOST_COMPLIANCE: string;
    public static DES_SERVICE_HOST_STORAGE: string;
    public static DES_SERVICE_HOST_PARTITION: string;
Diego Molteni's avatar
Diego Molteni committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    public static DES_SERVICE_APPKEY: string;
    public static DES_GROUP_CHAR_LIMIT: number;
    public static DE_FORWARD_APPKEY = Symbol('seismic-dms-fw-caller-appkey');

    // JWT Validation
    public static JWKS_URL: string;
    public static JWT_EXCLUDE_PATHS: string;
    public static JWT_AUDIENCE: string;
    public static JWT_ENABLE_FEATURE: boolean;

    // To set in the provider specific configurations based on implementation
    public static TENANT_JOURNAL_ON_DATA_PARTITION = false;

    // Feature Flags
    public static FEATURE_FLAG_AUTHORIZATION = true;
    public static FEATURE_FLAG_LEGALTAG = true;
    public static FEATURE_FLAG_SEISMICMETA_STORAGE = true;
    public static FEATURE_FLAG_IMPTOKEN = true;
    public static FEATURE_FLAG_STORAGE_CREDENTIALS = true;
    public static FEATURE_FLAG_TRACE = true;
    public static FEATURE_FLAG_LOGGING = true;
    public static FEATURE_FLAG_STACKDRIVER_EXPORTER = true;

    public static setCloudProvider(cloudProvider: string) {
        Config.CLOUDPROVIDER = cloudProvider;
        if (Config.CLOUDPROVIDER === undefined) {
            throw (new Error(
                'The \"CLOUDPROVIDER\" environment variable has not been set (required to start the server)'));
        }
    }

    protected static async initServiceConfiguration(model: ConfigModel): Promise<void> {

        Config.SERVICE_ENV = model.SERVICE_ENV;
        Config.SERVICE_PORT = model.SERVICE_PORT;

        Config.IMP_SERVICE_ACCOUNT_SIGNER = model.IMP_SERVICE_ACCOUNT_SIGNER;

        Config.LOCKSMAP_REDIS_INSTANCE_ADDRESS = model.LOCKSMAP_REDIS_INSTANCE_ADDRESS;
        Config.LOCKSMAP_REDIS_INSTANCE_PORT = model.LOCKSMAP_REDIS_INSTANCE_PORT;
        Config.LOCKSMAP_REDIS_INSTANCE_KEY = model.LOCKSMAP_REDIS_INSTANCE_KEY;

        Config.DES_REDIS_INSTANCE_ADDRESS =
            model.DES_REDIS_INSTANCE_ADDRESS || model.LOCKSMAP_REDIS_INSTANCE_ADDRESS;
        Config.DES_REDIS_INSTANCE_PORT =
            model.DES_REDIS_INSTANCE_PORT || model.LOCKSMAP_REDIS_INSTANCE_PORT;
        Config.DES_REDIS_INSTANCE_KEY =
            model.DES_REDIS_INSTANCE_KEY || model.LOCKSMAP_REDIS_INSTANCE_KEY;

        Config.FEATURE_FLAG_AUTHORIZATION = model.FEATURE_FLAG_AUTHORIZATION;
        Config.FEATURE_FLAG_LEGALTAG = model.FEATURE_FLAG_LEGALTAG;
        Config.FEATURE_FLAG_SEISMICMETA_STORAGE = model.FEATURE_FLAG_SEISMICMETA_STORAGE;
        Config.FEATURE_FLAG_IMPTOKEN = model.FEATURE_FLAG_IMPTOKEN;
        Config.FEATURE_FLAG_STORAGE_CREDENTIALS = model.FEATURE_FLAG_STORAGE_CREDENTIALS;
        Config.FEATURE_FLAG_TRACE = model.FEATURE_FLAG_TRACE;
        Config.FEATURE_FLAG_LOGGING = model.FEATURE_FLAG_LOGGING;
        Config.FEATURE_FLAG_STACKDRIVER_EXPORTER = model.FEATURE_FLAG_STACKDRIVER_EXPORTER;

165
166
167
168
        Config.DES_SERVICE_HOST_ENTITLEMENT = model.DES_SERVICE_HOST_ENTITLEMENT;
        Config.DES_SERVICE_HOST_COMPLIANCE = model.DES_SERVICE_HOST_COMPLIANCE;
        Config.DES_SERVICE_HOST_STORAGE = model.DES_SERVICE_HOST_STORAGE;
        Config.DES_SERVICE_HOST_PARTITION = model.DES_SERVICE_HOST_PARTITION;
Diego Molteni's avatar
Diego Molteni committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        Config.DES_SERVICE_APPKEY = model.DES_SERVICE_APPKEY;
        Config.DES_GROUP_CHAR_LIMIT = model.DES_GROUP_CHAR_LIMIT;

        Config.JWKS_URL = model.JWKS_URL
        Config.JWT_EXCLUDE_PATHS = model.JWT_EXCLUDE_PATHS
        Config.JWT_AUDIENCE = model.JWT_AUDIENCE
        Config.JWT_ENABLE_FEATURE = model.JWT_ENABLE_FEATURE

        Config.API_BASE_PATH = model.API_BASE_PATH;

        Config.TENANT_JOURNAL_ON_DATA_PARTITION = model.TENANT_JOURNAL_ON_DATA_PARTITION || false;

        Config.checkRequiredConfig(Config.CLOUDPROVIDER, 'CLOUDPROVIDER');
        Config.checkRequiredConfig(Config.SERVICE_ENV, 'SERVICE_ENV');
        Config.checkRequiredConfig(Config.IMP_SERVICE_ACCOUNT_SIGNER, 'IMP_SERVICE_ACCOUNT_SIGNER');
184
185
186
187
        Config.checkRequiredConfig(Config.DES_SERVICE_HOST_ENTITLEMENT, 'DES_SERVICE_HOST_ENTITLEMENT');
        Config.checkRequiredConfig(Config.DES_SERVICE_HOST_COMPLIANCE, 'DES_SERVICE_HOST_COMPLIANCE');
        Config.checkRequiredConfig(Config.DES_SERVICE_HOST_STORAGE, 'DES_SERVICE_HOST_STORAGE');
        Config.checkRequiredConfig(Config.DES_SERVICE_HOST_PARTITION, 'DES_SERVICE_HOST_PARTITION');
Diego Molteni's avatar
Diego Molteni committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
        Config.checkRequiredConfig(Config.DES_SERVICE_APPKEY, 'DES_SERVICE_APPKEY');

        // JWT validation
        if (Config.JWT_ENABLE_FEATURE) {
            Config.checkRequiredConfig(Config.JWKS_URL, 'JWKS_URL');
            Config.checkRequiredConfig(Config.JWT_EXCLUDE_PATHS, 'JWT_EXCLUDE_PATHS');
            Config.checkRequiredConfig(Config.JWT_AUDIENCE, 'JWT_AUDIENCE');
        }

        // autogenerated configurations
        Config.ORGANIZATION_NS = Config.ORGANIZATION_NS + '-' + Config.SERVICE_ENV;
        Config.SEISMIC_STORE_NS = Config.SEISMIC_STORE_NS + '-' + Config.SERVICE_ENV;

    }

    // must be implemented in the provider
    public abstract init(): Promise<void>;

    protected static checkRequiredConfig(config: any, name: string) {
        if (config === undefined || (typeof (config) === 'number' && isNaN(config))) {
            throw (new Error('missing configuration: ' + name));
        }
    }

}

export class ConfigFactory extends CloudFactory {
    public static build(providerLabel: string, args: { [key: string]: any } = {}): IConfig {
        return CloudFactory.build(providerLabel, Config, args) as IConfig;
    }
}

// Set the Utest flag correctly as sooon as the config class get loaded
221
222
223
224
Config.UTEST = process.env.UTEST;

// Set the CACHE(REDIS) TLS DISABLE flag correctly. If not SET TLS is assume enabled so that change is non breaking
Config.CACHE_TLS_DISABLE = process.env.CACHE_TLS_DISABLE;