"use strict";
/*
 *  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  SPDX-License-Identifier: Apache-2.0
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const swb_app_1 = require("@aws/swb-app");
const workbench_core_accounts_1 = require("@aws/workbench-core-accounts");
const workbench_core_audit_1 = require("@aws/workbench-core-audit");
const workbench_core_authorization_1 = require("@aws/workbench-core-authorization");
const workbench_core_base_1 = require("@aws/workbench-core-base");
const workbench_core_datasets_1 = require("@aws/workbench-core-datasets");
const workbench_core_environments_1 = require("@aws/workbench-core-environments");
const workbench_core_logging_1 = require("@aws/workbench-core-logging");
const workbench_core_user_management_1 = require("@aws/workbench-core-user-management");
const constants_1 = require("./constants");
const DynamicRouteConfig = __importStar(require("./dynamicRouteConfig"));
const sagemakerNotebookEnvironmentConnectionService_1 = __importDefault(require("./environment/sagemakerNotebook/sagemakerNotebookEnvironmentConnectionService"));
const sagemakerNotebookEnvironmentLifecycleService_1 = __importDefault(require("./environment/sagemakerNotebook/sagemakerNotebookEnvironmentLifecycleService"));
const databaseService_1 = require("./services/databaseService");
const dataSetService_1 = require("./services/dataSetService");
const envTypeConfigService_1 = require("./services/envTypeConfigService");
const projectEnvService_1 = require("./services/projectEnvService");
const projectEnvTypeConfigService_1 = require("./services/projectEnvTypeConfigService");
const sshKeyService_1 = __importDefault(require("./services/sshKeyService"));
const swbEnvironmentService_1 = require("./services/swbEnvironmentService");
const swbProjectService_1 = require("./services/swbProjectService");
const requiredAuditValues = ['actor', 'source'];
const fieldsToMask = JSON.parse(process.env.FIELDS_TO_MASK_WHEN_AUDITING);
const logger = new workbench_core_logging_1.LoggingService();
const aws = new workbench_core_base_1.AwsService({
    region: process.env.AWS_REGION,
    userAgent: process.env.USER_AGENT_STRING,
    ddbTableName: process.env.STACK_NAME
});
// Dynamic Auth
const dynamicAuthAws = new workbench_core_base_1.AwsService({
    region: process.env.AWS_REGION,
    userAgent: process.env.USER_AGENT_STRING,
    ddbTableName: process.env.DYNAMIC_AUTH_DDB_TABLE_NAME
});
const cognitoUserManagementPlugin = new workbench_core_user_management_1.CognitoUserManagementPlugin(process.env.USER_POOL_ID, aws, {
    ddbService: dynamicAuthAws.helpers.ddb,
    ttl: 60 * 60
});
const userManagementService = new workbench_core_user_management_1.UserManagementService(cognitoUserManagementPlugin);
const wbcGroupManagementPlugin = new workbench_core_authorization_1.WBCGroupManagementPlugin({
    userManagementService,
    ddbService: dynamicAuthAws.helpers.ddb,
    userGroupKeyType: constants_1.authorizationGroupPrefix
});
const dynamicRoutesMap = DynamicRouteConfig.dynamicRoutesMap;
const routesIgnored = DynamicRouteConfig.routesIgnored;
const ddbDynamicAuthorizationPermissionsPlugin = new workbench_core_authorization_1.DDBDynamicAuthorizationPermissionsPlugin({
    dynamoDBService: dynamicAuthAws.helpers.ddb,
    dynamicRoutesMap,
    routesIgnored
});
const caslAuthorizationPlugin = new workbench_core_authorization_1.CASLAuthorizationPlugin();
const dynamicAuthorizationService = new workbench_core_authorization_1.DynamicAuthorizationService({
    groupManagementPlugin: wbcGroupManagementPlugin,
    dynamicAuthorizationPermissionsPlugin: ddbDynamicAuthorizationPermissionsPlugin,
    auditService: new workbench_core_audit_1.AuditService(new workbench_core_audit_1.BaseAuditPlugin(new workbench_core_audit_1.AuditLogger(logger))),
    authorizationPlugin: caslAuthorizationPlugin
});
const accountService = new workbench_core_accounts_1.AccountService(aws.helpers.ddb);
const environmentService = new workbench_core_environments_1.EnvironmentService(aws.helpers.ddb);
const envTypeService = new workbench_core_environments_1.EnvironmentTypeService(aws.helpers.ddb);
const envTypeConfigService = new workbench_core_environments_1.EnvironmentTypeConfigService(envTypeService, aws.helpers.ddb);
const metadataService = new workbench_core_base_1.MetadataService(aws.helpers.ddb);
const costCenterService = new workbench_core_accounts_1.CostCenterService(aws.helpers.ddb);
const projectService = new workbench_core_accounts_1.ProjectService(aws.helpers.ddb, costCenterService);
const apiRouteConfig = {
    environments: {
        sagemakerNotebook: {
            lifecycle: new sagemakerNotebookEnvironmentLifecycleService_1.default(),
            connection: new sagemakerNotebookEnvironmentConnectionService_1.default()
        }
        // Add your environment types here as follows:
        // <newEnvTypeName>: {
        //   lifecycle: new <newEnvTypeName>EnvironmentLifecycleService(),
        //   connection: new <newEnvTypeName>EnvironmentConnectionService()
        // }
    },
    account: new workbench_core_accounts_1.HostingAccountService(new workbench_core_accounts_1.HostingAccountLifecycleService(process.env.STACK_NAME, aws, accountService)),
    dataSetService: new dataSetService_1.DataSetService(new workbench_core_datasets_1.S3DataSetStoragePlugin(aws), new workbench_core_datasets_1.DataSetService(new workbench_core_audit_1.AuditService(new workbench_core_audit_1.BaseAuditPlugin(new workbench_core_audit_1.AuditLogger(logger)), true, requiredAuditValues, fieldsToMask), logger, new workbench_core_datasets_1.DdbDataSetMetadataPlugin(aws, constants_1.dataSetPrefix, constants_1.endPointPrefix, constants_1.storageLocationPrefix), new workbench_core_datasets_1.WbcDataSetsAuthorizationPlugin(dynamicAuthorizationService)), new workbench_core_datasets_1.WbcDataSetsAuthorizationPlugin(dynamicAuthorizationService), new databaseService_1.DatabaseService(), dynamicAuthorizationService),
    allowedOrigins: JSON.parse(process.env.ALLOWED_ORIGINS || '[]'),
    environmentService,
    environmentPlugin: new swbEnvironmentService_1.SWBEnvironmentService(environmentService),
    environmentTypeService: envTypeService,
    environmentTypeConfigService: new envTypeConfigService_1.EnvTypeConfigService(envTypeConfigService, metadataService),
    userManagementService: userManagementService,
    costCenterService: new workbench_core_accounts_1.CostCenterService(aws.helpers.ddb),
    metadataService: metadataService,
    projectEnvPlugin: new projectEnvService_1.ProjectEnvService(dynamicAuthorizationService, environmentService, projectService),
    projectEnvTypeConfigPlugin: new projectEnvTypeConfigService_1.ProjectEnvTypeConfigService(metadataService, projectService, envTypeConfigService, envTypeService, environmentService, dynamicAuthorizationService),
    projectPlugin: new swbProjectService_1.SWBProjectService(dynamicAuthorizationService, projectService),
    projectService: projectService,
    sshKeyService: new sshKeyService_1.default(aws, projectService, environmentService),
    authorizationService: dynamicAuthorizationService,
    routesIgnored: DynamicRouteConfig.routesIgnored
};
const backendAPIApp = (0, swb_app_1.generateRouter)(apiRouteConfig);
exports.default = backendAPIApp;
//# sourceMappingURL=backendAPI.js.map