@@ -6,13 +6,31 @@ import * as mongo from './mongo';
66import * as neDb from './file' ;
77import { Action } from '../proxy/actions/Action' ;
88import MongoDBStore from 'connect-mongo' ;
9-
10- let sink : Sink ;
11- if ( config . getDatabase ( ) . type === 'mongo' ) {
12- sink = mongo ;
13- } else if ( config . getDatabase ( ) . type === 'fs' ) {
14- sink = neDb ;
15- }
9+ import { processGitUrl } from '../proxy/routes/helper' ;
10+ import { initializeFolders } from './file/helper' ;
11+
12+ let _sink : Sink | null = null ;
13+
14+ /** The start function is before any attempt to use the DB adaptor and causes the configuration
15+ * to be read. This allows the read of the config to be deferred, otherwise it will occur on
16+ * import.
17+ */
18+ const start = ( ) => {
19+ if ( ! _sink ) {
20+ if ( config . getDatabase ( ) . type === 'mongo' ) {
21+ console . log ( 'Loading MongoDB database adaptor' ) ;
22+ _sink = mongo ;
23+ } else if ( config . getDatabase ( ) . type === 'fs' ) {
24+ console . log ( 'Loading neDB database adaptor' ) ;
25+ initializeFolders ( ) ;
26+ _sink = neDb ;
27+ } else {
28+ console . error ( `Unsupported database type: ${ config . getDatabase ( ) . type } ` ) ;
29+ process . exit ( 1 ) ;
30+ }
31+ }
32+ return _sink ;
33+ } ;
1634
1735const isBlank = ( str : string ) => {
1836 return ! str || / ^ \s * $ / . test ( str ) ;
@@ -57,6 +75,7 @@ export const createUser = async (
5775 const errorMessage = `email cannot be empty` ;
5876 throw new Error ( errorMessage ) ;
5977 }
78+ const sink = start ( ) ;
6079 const existingUser = await sink . findUser ( username ) ;
6180 if ( existingUser ) {
6281 const errorMessage = `user ${ username } already exists` ;
@@ -95,7 +114,7 @@ export const createRepo = async (repo: AuthorisedRepo) => {
95114 throw new Error ( 'URL cannot be empty' ) ;
96115 }
97116
98- return sink . createRepo ( toCreate ) as Promise < Required < Repo > > ;
117+ return start ( ) . createRepo ( toCreate ) as Promise < Required < Repo > > ;
99118} ;
100119
101120export const isUserPushAllowed = async ( url : string , user : string ) => {
@@ -114,7 +133,7 @@ export const canUserApproveRejectPush = async (id: string, user: string) => {
114133 return false ;
115134 }
116135
117- const theRepo = await sink . getRepoByUrl ( action . url ) ;
136+ const theRepo = await start ( ) . getRepoByUrl ( action . url ) ;
118137
119138 if ( theRepo ?. users ?. canAuthorise ?. includes ( user ) ) {
120139 console . log ( `user ${ user } can approve/reject for repo ${ action . url } ` ) ;
@@ -140,35 +159,55 @@ export const canUserCancelPush = async (id: string, user: string) => {
140159 }
141160} ;
142161
143- export const getSessionStore = ( ) : MongoDBStore | undefined =>
144- sink . getSessionStore ? sink . getSessionStore ( ) : undefined ;
145- export const getPushes = ( query : Partial < PushQuery > ) : Promise < Action [ ] > => sink . getPushes ( query ) ;
146- export const writeAudit = ( action : Action ) : Promise < void > => sink . writeAudit ( action ) ;
147- export const getPush = ( id : string ) : Promise < Action | null > => sink . getPush ( id ) ;
148- export const deletePush = ( id : string ) : Promise < void > => sink . deletePush ( id ) ;
162+ export const getSessionStore = ( ) : MongoDBStore | undefined => start ( ) . getSessionStore ( ) ;
163+ export const getPushes = ( query : Partial < PushQuery > ) : Promise < Action [ ] > => start ( ) . getPushes ( query ) ;
164+ export const writeAudit = ( action : Action ) : Promise < void > => start ( ) . writeAudit ( action ) ;
165+ export const getPush = ( id : string ) : Promise < Action | null > => start ( ) . getPush ( id ) ;
166+ export const deletePush = ( id : string ) : Promise < void > => start ( ) . deletePush ( id ) ;
149167export const authorise = ( id : string , attestation : any ) : Promise < { message : string } > =>
150- sink . authorise ( id , attestation ) ;
151- export const cancel = ( id : string ) : Promise < { message : string } > => sink . cancel ( id ) ;
168+ start ( ) . authorise ( id , attestation ) ;
169+ export const cancel = ( id : string ) : Promise < { message : string } > => start ( ) . cancel ( id ) ;
152170export const reject = ( id : string , attestation : any ) : Promise < { message : string } > =>
153- sink . reject ( id , attestation ) ;
154- export const getRepos = ( query ?: Partial < RepoQuery > ) : Promise < Repo [ ] > => sink . getRepos ( query ) ;
155- export const getRepo = ( name : string ) : Promise < Repo | null > => sink . getRepo ( name ) ;
156- export const getRepoByUrl = ( url : string ) : Promise < Repo | null > => sink . getRepoByUrl ( url ) ;
157- export const getRepoById = ( _id : string ) : Promise < Repo | null > => sink . getRepoById ( _id ) ;
171+ start ( ) . reject ( id , attestation ) ;
172+ export const getRepos = ( query ?: Partial < RepoQuery > ) : Promise < Repo [ ] > => start ( ) . getRepos ( query ) ;
173+ export const getRepo = ( name : string ) : Promise < Repo | null > => start ( ) . getRepo ( name ) ;
174+ export const getRepoByUrl = ( url : string ) : Promise < Repo | null > => start ( ) . getRepoByUrl ( url ) ;
175+ export const getRepoById = ( _id : string ) : Promise < Repo | null > => start ( ) . getRepoById ( _id ) ;
158176export const addUserCanPush = ( _id : string , user : string ) : Promise < void > =>
159- sink . addUserCanPush ( _id , user ) ;
177+ start ( ) . addUserCanPush ( _id , user ) ;
160178export const addUserCanAuthorise = ( _id : string , user : string ) : Promise < void > =>
161- sink . addUserCanAuthorise ( _id , user ) ;
179+ start ( ) . addUserCanAuthorise ( _id , user ) ;
162180export const removeUserCanPush = ( _id : string , user : string ) : Promise < void > =>
163- sink . removeUserCanPush ( _id , user ) ;
181+ start ( ) . removeUserCanPush ( _id , user ) ;
164182export const removeUserCanAuthorise = ( _id : string , user : string ) : Promise < void > =>
165- sink . removeUserCanAuthorise ( _id , user ) ;
166- export const deleteRepo = ( _id : string ) : Promise < void > => sink . deleteRepo ( _id ) ;
167- export const findUser = ( username : string ) : Promise < User | null > => sink . findUser ( username ) ;
168- export const findUserByEmail = ( email : string ) : Promise < User | null > => sink . findUserByEmail ( email ) ;
169- export const findUserByOIDC = ( oidcId : string ) : Promise < User | null > => sink . findUserByOIDC ( oidcId ) ;
170- export const getUsers = ( query ?: Partial < UserQuery > ) : Promise < User [ ] > => sink . getUsers ( query ) ;
171- export const deleteUser = ( username : string ) : Promise < void > => sink . deleteUser ( username ) ;
172-
173- export const updateUser = ( user : Partial < User > ) : Promise < void > => sink . updateUser ( user ) ;
183+ start ( ) . removeUserCanAuthorise ( _id , user ) ;
184+ export const deleteRepo = ( _id : string ) : Promise < void > => start ( ) . deleteRepo ( _id ) ;
185+ export const findUser = ( username : string ) : Promise < User | null > => start ( ) . findUser ( username ) ;
186+ export const findUserByEmail = ( email : string ) : Promise < User | null > =>
187+ start ( ) . findUserByEmail ( email ) ;
188+ export const findUserByOIDC = ( oidcId : string ) : Promise < User | null > =>
189+ start ( ) . findUserByOIDC ( oidcId ) ;
190+ export const getUsers = ( query ?: Partial < UserQuery > ) : Promise < User [ ] > => start ( ) . getUsers ( query ) ;
191+ export const deleteUser = ( username : string ) : Promise < void > => start ( ) . deleteUser ( username ) ;
192+
193+ export const updateUser = ( user : Partial < User > ) : Promise < void > => start ( ) . updateUser ( user ) ;
194+ /**
195+ * Collect the Set of all host (host and port if specified) that we
196+ * will be proxying requests for, to be used to initialize the proxy.
197+ *
198+ * @return {string[] } an array of origins
199+ */
200+
201+ export const getAllProxiedHosts = async ( ) : Promise < string [ ] > => {
202+ const repos = await getRepos ( ) ;
203+ const origins = new Set < string > ( ) ;
204+ repos . forEach ( ( repo ) => {
205+ const parsedUrl = processGitUrl ( repo . url ) ;
206+ if ( parsedUrl ) {
207+ origins . add ( parsedUrl . host ) ;
208+ } // failures are logged by parsing util fn
209+ } ) ;
210+ return Array . from ( origins ) ;
211+ } ;
212+
174213export type { PushQuery , Repo , Sink , User } from './types' ;
0 commit comments