@@ -24,150 +24,150 @@ export class Auth {
2424 }
2525 }
2626
27- public static item_cache : BaseObserver = null ;
27+ // public static item_cache: BaseObserver = null;
2828
29- public static authorizationCache : HashTable < CachedUser > = { } ;
30- private static cacheTimer : NodeJS . Timeout ;
31- public static shutdown ( ) {
32- if ( ! NoderedUtil . IsNullUndefinded ( this . cacheTimer ) ) {
33- clearInterval ( this . cacheTimer ) ;
34- }
35- }
36- public static ensureotel ( ) {
37- if ( ! NoderedUtil . IsNullUndefinded ( Logger . otel ) && NoderedUtil . IsNullUndefinded ( Auth . item_cache ) ) {
38- Auth . item_cache = Logger . otel . meter . createValueObserver ( "openflow_item_cache_count" , {
39- description : 'Total number of cached items'
40- } , res => {
41- let keys : string [ ] = Object . keys ( this . authorizationCache ) ;
42- let types = { } ;
43- for ( let i = keys . length - 1 ; i >= 0 ; i -- ) {
44- if ( ! types [ this . authorizationCache [ keys [ i ] ] . type ] ) types [ this . authorizationCache [ keys [ i ] ] . type ] = 0 ;
45- types [ this . authorizationCache [ keys [ i ] ] . type ] ++ ;
46- }
47- keys = Object . keys ( types ) ;
48- for ( let i = keys . length - 1 ; i >= 0 ; i -- ) {
49- res . observe ( types [ keys [ i ] ] , { ...Logger . otel . defaultlabels , type : keys [ i ] } )
50- }
51- } ) ;
52- }
53- }
54- public static getUser ( key : string , type : string ) : User {
55- if ( NoderedUtil . IsNullUndefinded ( this . cacheTimer ) ) this . cacheTimer = setInterval ( this . cleanCache , 60000 )
56- var res : CachedUser = this . authorizationCache [ key + type ] ;
57- if ( res === null || res === undefined ) {
58- if ( Config . log_cache ) Logger . instanse . debug ( "[" + type + "][" + key + "] not found in cache" ) ;
59- return null ;
60- }
61- var begin : number = res . firstsignin . getTime ( ) ;
62- var end : number = new Date ( ) . getTime ( ) ;
63- var seconds = Math . round ( ( end - begin ) / 1000 ) ;
64- let cache_seconds : number = Config . api_credential_cache_seconds ;
65- if ( type == "grafana" ) cache_seconds = Config . grafana_credential_cache_seconds ;
66- if ( type == "dashboard" ) cache_seconds = Config . dashboard_credential_cache_seconds ;
67- if ( type == "cleanacl" ) cache_seconds = Config . cleanacl_credential_cache_seconds ;
68- if ( type == "userroles" ) cache_seconds = Config . cleanacl_credential_cache_seconds ;
69- if ( type == "mq" ) cache_seconds = Config . mq_credential_cache_seconds ;
70- if ( type == "mqe" ) cache_seconds = Config . mq_credential_cache_seconds ;
71- if ( type == "password" ) cache_seconds = Config . cleanacl_credential_cache_seconds ;
72- if ( seconds < cache_seconds ) {
73- Logger . instanse . silly ( "Return user " + res . user . username + " from cache" ) ;
74- return res . user as User ;
75- }
76- this . RemoveUser ( key , type ) ;
77- return null ;
78- }
79- public static async clearCache ( reason : string ) {
80- DBHelper . cached_roles = [ ] ;
81- if ( this . authorizationCache == null || this . authorizationCache == { } ) {
82- if ( Config . log_cache ) Logger . instanse . debug ( "clearCache called, but cache was empty, reason: " + reason ) ;
83- return ;
84- }
85- Auth . ensureotel ( ) ;
86- let keys : string [ ] = Object . keys ( this . authorizationCache ) ;
87- await Auth . semaphore . down ( ) ;
88- this . authorizationCache = { }
89- Auth . semaphore . up ( ) ;
90- if ( Config . log_cache ) Logger . instanse . debug ( "clearCache called with " + keys . length + " keys in cache, reason: " + reason ) ;
91- }
92- public static async cleanCache ( ) {
93- try {
94- if ( this . authorizationCache == null ) return ;
95- const keys : string [ ] = Object . keys ( this . authorizationCache ) ;
96- for ( let i = keys . length - 1 ; i >= 0 ; i -- ) {
97- let key : string = keys [ i ] ;
98- var res : CachedUser = this . authorizationCache [ key ] ;
99- if ( res === null || res === undefined ) continue ;
100- var begin : number = res . firstsignin . getTime ( ) ;
101- var end : number = new Date ( ) . getTime ( ) ;
102- var seconds = Math . round ( ( end - begin ) / 1000 ) ;
103- let cache_seconds : number = Config . api_credential_cache_seconds ;
104- if ( res . type == "grafana" ) cache_seconds = Config . grafana_credential_cache_seconds ;
105- if ( res . type == "dashboard" ) cache_seconds = Config . dashboard_credential_cache_seconds ;
106- if ( res . type == "cleanacl" ) cache_seconds = Config . cleanacl_credential_cache_seconds ;
107- if ( res . type == "userroles" ) cache_seconds = Config . cleanacl_credential_cache_seconds ;
108- if ( res . type == "mq" ) cache_seconds = Config . mq_credential_cache_seconds ;
109- if ( res . type == "mqe" ) cache_seconds = Config . mq_credential_cache_seconds ;
110- if ( res . type == "password" ) cache_seconds = Config . cleanacl_credential_cache_seconds ;
111- if ( seconds >= cache_seconds ) {
112- this . RemoveUser ( key , res . type ) ;
113- }
114- }
115- const keys2 : string [ ] = Object . keys ( this . authorizationCache ) ;
116- if ( Config . log_cache ) Logger . instanse . debug ( "cleanCache called with " + keys . length + " keys in cache, and " + keys2 . length + " after enumeration" ) ;
117- } catch ( error ) {
118- Logger . instanse . error ( error )
119- }
120- }
121- public static async RemoveUser ( key : string , type : string ) : Promise < void > {
122- Auth . ensureotel ( ) ;
123- await Auth . semaphore . down ( ) ;
124- if ( ! NoderedUtil . IsNullUndefinded ( this . authorizationCache [ key + type ] ) ) {
125- if ( Config . log_cache ) Logger . instanse . debug ( "Delete user with key " + key + " from cache" ) ;
126- delete this . authorizationCache [ key + type ] ;
127- } else {
128- if ( Config . log_cache ) Logger . instanse . debug ( "RemoveUser user called with " + key + " but was not found in cache" ) ;
129- }
130- Auth . semaphore . up ( ) ;
131- }
132- public static async AddUser ( user : User | TokenUser , key : string , type : string ) : Promise < void > {
133- Auth . ensureotel ( ) ;
134- await Auth . semaphore . down ( ) ;
135- if ( NoderedUtil . IsNullUndefinded ( this . authorizationCache [ key + type ] ) ) {
136- if ( Config . log_cache ) Logger . instanse . debug ( "Adding user " + user . name + " to cache with key " + key ) ;
137- } else {
138- if ( Config . log_cache ) Logger . instanse . debug ( "Updating user " + user . name + " to cache with key " + key ) ;
139- }
140- var cuser : CachedUser = new CachedUser ( user , user . _id , type ) ;
141- this . authorizationCache [ key + type ] = cuser ;
142- Auth . semaphore . up ( ) ;
143- }
144- public static Semaphore = ( n ) => ( {
145- n,
146- async down ( ) {
147- while ( this . n <= 0 ) await this . wait ( ) ;
148- this . n -- ;
149- } ,
150- up ( ) {
151- this . n ++ ;
152- } ,
153- async wait ( ) {
154- if ( this . n <= 0 ) return new Promise ( ( res , req ) => {
155- setImmediate ( async ( ) => res ( await this . wait ( ) ) )
156- } ) ;
157- } ,
158- } ) ;
159- public static semaphore = Auth . Semaphore ( 1 ) ;
160- }
161- export class CachedUser {
162- public firstsignin : Date ;
163- constructor (
164- public user : User | TokenUser ,
165- public _id : string ,
166- public type : string
167- ) {
168- this . firstsignin = new Date ( ) ;
169- }
170- }
171- interface HashTable < T > {
172- [ key : string ] : T ;
29+ // public static authorizationCache: HashTable<CachedUser> = {};
30+ // private static cacheTimer: NodeJS.Timeout;
31+ // public static shutdown() {
32+ // if (!NoderedUtil.IsNullUndefinded(this.cacheTimer)) {
33+ // clearInterval(this.cacheTimer);
34+ // }
35+ // }
36+ // public static ensureotel() {
37+ // if (!NoderedUtil.IsNullUndefinded(Logger.otel) && NoderedUtil.IsNullUndefinded(Auth.item_cache)) {
38+ // Auth.item_cache = Logger.otel.meter.createValueObserver("openflow_item_cache_count", {
39+ // description: 'Total number of cached items'
40+ // }, res => {
41+ // let keys: string[] = Object.keys(this.authorizationCache);
42+ // let types = {};
43+ // for (let i = keys.length - 1; i >= 0; i--) {
44+ // if (!types[this.authorizationCache[keys[i]].type]) types[this.authorizationCache[keys[i]].type] = 0;
45+ // types[this.authorizationCache[keys[i]].type]++;
46+ // }
47+ // keys = Object.keys(types);
48+ // for (let i = keys.length - 1; i >= 0; i--) {
49+ // res.observe(types[keys[i]], { ...Logger.otel.defaultlabels, type: keys[i] })
50+ // }
51+ // });
52+ // }
53+ // }
54+ // public static getUser(key: string, type: string): User {
55+ // if (NoderedUtil.IsNullUndefinded(this.cacheTimer)) this.cacheTimer = setInterval(this.cleanCache, 60000)
56+ // var res: CachedUser = this.authorizationCache[key + type];
57+ // if (res === null || res === undefined) {
58+ // if (Config.log_cache) Logger.instanse.debug("[" + type + "][" + key + "] not found in cache");
59+ // return null;
60+ // }
61+ // var begin: number = res.firstsignin.getTime();
62+ // var end: number = new Date().getTime();
63+ // var seconds = Math.round((end - begin) / 1000);
64+ // let cache_seconds: number = Config.api_credential_cache_seconds;
65+ // if (type == "grafana") cache_seconds = Config.grafana_credential_cache_seconds;
66+ // if (type == "dashboard") cache_seconds = Config.dashboard_credential_cache_seconds;
67+ // if (type == "cleanacl") cache_seconds = Config.cleanacl_credential_cache_seconds;
68+ // if (type == "userroles") cache_seconds = Config.cleanacl_credential_cache_seconds;
69+ // if (type == "mq") cache_seconds = Config.mq_credential_cache_seconds;
70+ // if (type == "mqe") cache_seconds = Config.mq_credential_cache_seconds;
71+ // if (type == "password") cache_seconds = Config.cleanacl_credential_cache_seconds;
72+ // if (seconds < cache_seconds) {
73+ // Logger.instanse.silly("Return user " + res.user.username + " from cache");
74+ // return res.user as User;
75+ // }
76+ // this.RemoveUser(key, type);
77+ // return null;
78+ // }
79+ // public static async clearCache(reason: string) {
80+ // // DBHelper.cached_roles = [];
81+ // if (this.authorizationCache == null || this.authorizationCache == {}) {
82+ // if (Config.log_cache) Logger.instanse.debug("clearCache called, but cache was empty, reason: " + reason);
83+ // return;
84+ // }
85+ // Auth.ensureotel();
86+ // let keys: string[] = Object.keys(this.authorizationCache);
87+ // await Auth.semaphore.down();
88+ // this.authorizationCache = {}
89+ // Auth.semaphore.up();
90+ // if (Config.log_cache) Logger.instanse.debug("clearCache called with " + keys.length + " keys in cache, reason: " + reason);
91+ // }
92+ // public static async cleanCache() {
93+ // try {
94+ // if (this.authorizationCache == null) return;
95+ // const keys: string[] = Object.keys(this.authorizationCache);
96+ // for (let i = keys.length - 1; i >= 0; i--) {
97+ // let key: string = keys[i];
98+ // var res: CachedUser = this.authorizationCache[key];
99+ // if (res === null || res === undefined) continue;
100+ // var begin: number = res.firstsignin.getTime();
101+ // var end: number = new Date().getTime();
102+ // var seconds = Math.round((end - begin) / 1000);
103+ // let cache_seconds: number = Config.api_credential_cache_seconds;
104+ // if (res.type == "grafana") cache_seconds = Config.grafana_credential_cache_seconds;
105+ // if (res.type == "dashboard") cache_seconds = Config.dashboard_credential_cache_seconds;
106+ // if (res.type == "cleanacl") cache_seconds = Config.cleanacl_credential_cache_seconds;
107+ // if (res.type == "userroles") cache_seconds = Config.cleanacl_credential_cache_seconds;
108+ // if (res.type == "mq") cache_seconds = Config.mq_credential_cache_seconds;
109+ // if (res.type == "mqe") cache_seconds = Config.mq_credential_cache_seconds;
110+ // if (res.type == "password") cache_seconds = Config.cleanacl_credential_cache_seconds;
111+ // if (seconds >= cache_seconds) {
112+ // this.RemoveUser(key, res.type);
113+ // }
114+ // }
115+ // const keys2: string[] = Object.keys(this.authorizationCache);
116+ // if (Config.log_cache) Logger.instanse.debug("cleanCache called with " + keys.length + " keys in cache, and " + keys2.length + " after enumeration");
117+ // } catch (error) {
118+ // Logger.instanse.error(error)
119+ // }
120+ // }
121+ // public static async RemoveUser(key: string, type: string): Promise<void> {
122+ // Auth.ensureotel();
123+ // await Auth.semaphore.down();
124+ // if (!NoderedUtil.IsNullUndefinded(this.authorizationCache[key + type])) {
125+ // if (Config.log_cache) Logger.instanse.debug("Delete user with key " + key + " from cache");
126+ // delete this.authorizationCache[key + type];
127+ // } else {
128+ // if (Config.log_cache) Logger.instanse.debug("RemoveUser user called with " + key + " but was not found in cache");
129+ // }
130+ // Auth.semaphore.up();
131+ // }
132+ // public static async AddUser(user: User | TokenUser, key: string, type: string): Promise<void> {
133+ // Auth.ensureotel();
134+ // await Auth.semaphore.down();
135+ // if (NoderedUtil.IsNullUndefinded(this.authorizationCache[key + type])) {
136+ // if (Config.log_cache) Logger.instanse.debug("Adding user " + user.name + " to cache with key " + key);
137+ // } else {
138+ // if (Config.log_cache) Logger.instanse.debug("Updating user " + user.name + " to cache with key " + key);
139+ // }
140+ // var cuser: CachedUser = new CachedUser(user, user._id, type);
141+ // this.authorizationCache[key + type] = cuser;
142+ // Auth.semaphore.up();
143+ // }
144+ // public static Semaphore = (n) => ({
145+ // n,
146+ // async down() {
147+ // while (this.n <= 0) await this.wait();
148+ // this.n--;
149+ // },
150+ // up() {
151+ // this.n++;
152+ // },
153+ // async wait() {
154+ // if (this.n <= 0) return new Promise((res, req) => {
155+ // setImmediate(async () => res(await this.wait()))
156+ // });
157+ // },
158+ // });
159+ // public static semaphore = Auth.Semaphore(1);
173160}
161+ // export class CachedUser {
162+ // public firstsignin: Date;
163+ // constructor(
164+ // public user: User | TokenUser,
165+ // public _id: string,
166+ // public type: string
167+ // ) {
168+ // this.firstsignin = new Date();
169+ // }
170+ // }
171+ // interface HashTable<T> {
172+ // [key: string]: T;
173+ // }
0 commit comments