import Debug from 'debug' ; import { Events , REPL } from '@kui-shell/core' ; import { fetchFileString } from '@kui-shell/plugin-kubectl' ; import Provider , { ProviderInitializer } from './model' ; const debug = Debug ( 'plugin-s3/providers/local-minio' ) ; const localStorageAccessKey = 'kui-shell/plugin-s3/minio/accessKey' ; const localStorageSecretKey = 'kui-shell/plugin-s3/minio/secretKey' ; const mountName = 'minio' ; class LocalMinioS3Provider implements Provider { public readonly mountName = mountName ; public readonly endPoint = '127.0.0.1' ; public readonly port = 9000 ; public readonly useSSL = false ; public readonly accessKey = process . env . MINIO_ACCESS_KEY || localStorage . getItem ( localStorageAccessKey ) || 'minioadmin' ; public readonly secretKey = process . env . MINIO_SECRET_KEY || localStorage . getItem ( localStorageSecretKey ) || 'minioadmin' ; /** In case of setup/connection errors */ public error : Error = undefined ; } /** Listening for reconfigs? */ const listeningAlready = false ; /** Avoid repeated warnings about inability to connect to minio */ let alreadyReportedCannotConnect = false ; async function init ( repl , reinit ) { if ( ! listeningAlready ) { Events . eventBus . onEnvUpdate ( 'MINIO_ACCESS_KEY' , value => { localStorage . setItem ( localStorageAccessKey , value ) ; reinit ( ) ; } ) ; Events . eventBus . onEnvUpdate ( 'MINIO_SECRET_KEY' , value => { localStorage . setItem ( localStorageSecretKey , value ) ; reinit ( ) ; } ) ; } const provider = new LocalMinioS3Provider ( ) ; if ( provider . accessKey && provider . secretKey ) { try { await fetchFileString ( { REPL : repl } , `${ provider . useSSL ? 'https' : 'http' }://${ provider . endPoint }:${ provider . port }/minio/health/live` ) ; alreadyReportedCannotConnect = false ; } catch ( error ) { if ( ! alreadyReportedCannotConnect ) { debug ( 'Cannot connect to minio' , error ) ; alreadyReportedCannotConnect = true ; } provider . error = error ; } return provider ; } } const initializer = { init , mountName } ; export default initializer ;