• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

janis-commerce / lambda / 7747286274

01 Feb 2024 08:31PM CUT coverage: 100.0%. Remained the same
7747286274

Pull #15

github

jormaechea
Added SF context object docs
Pull Request #15: Jcn 469 implement state event parameter

263 of 263 branches covered (100.0%)

Branch coverage included in aggregate %.

12 of 12 new or added lines in 4 files covered. (100.0%)

401 of 401 relevant lines covered (100.0%)

17.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lib/helpers/lambda-instance.js
1
'use strict';
2

3
const logger = require('lllog')();
1✔
4

5
const { StsWrapper, LambdaWrapper } = require('./aws-wrappers');
1✔
6

7
const LambdaError = require('../lambda-error');
1✔
8

9
const LOCAL_ENDPOINT_BASE = 'http://janis-{serviceCode}:{MS_PORT}/';
1✔
10

11
module.exports = class LambdaInstance {
1✔
12

13
        /**
14
         * @private
15
         * @static
16
         */
17
        static get roleName() {
18
                return 'LambdaRemoteInvoke';
8✔
19
        }
20

21
        /**
22
         * @private
23
         * @static
24
         */
25
        static get roleSessionName() {
26
                return process.env.JANIS_SERVICE_NAME;
9✔
27
        }
28

29
        /**
30
         * @private
31
         * @static
32
         */
33
        static get roleSessionDuration() {
34
                return 1800; // 30 minutes
9✔
35
        }
36

37
        /**
38
         * @private
39
         * @static
40
         */
41
        static get sts() {
42

43
                if(!this._sts)
8✔
44
                        this._sts = new StsWrapper();
6✔
45

46
                return this._sts;
8✔
47
        }
48

49
        /**
50
         * @private
51
         * @static
52
         * @param {object} options
53
         * @param {boolean} options.useLocalEndpoint If true, will use local Janis Service endpoint
54
         */
55
        static getInstance({ useLocalEndpoint } = {}) {
3✔
56

57
                if(!this._basicInstance)
51✔
58
                        this._basicInstance = new LambdaWrapper(useLocalEndpoint && { endpoint: this.buildLocalEndpoint(process.env.MS_PORT) });
35✔
59

60
                return this._basicInstance;
51✔
61
        }
62

63
        /**
64
         * @private
65
         * @static
66
         */
67
        static buildLocalEndpoint(port, serviceCode = process.env.JANIS_SERVICE_NAME) {
4✔
68
                // A combination of the port plus the number 2 will be used since the sls-helper-plugin-janis package
69
                // is the one that generates the port
70
                return LOCAL_ENDPOINT_BASE.replace('{MS_PORT}', `2${port}`).replace('{serviceCode}', serviceCode);
6✔
71
        }
72

73
        /**
74
         * @private
75
         * @static
76
         * @param {string} accountId JANIS Service Account ID
77
         * @returns {Lambda} Lambda instance with role credentials
78
         */
79
        static async getInstanceWithRole(accountId) {
80

81
                const cachedInstance = this.getCachedInstance(accountId);
9✔
82

83
                if(cachedInstance)
9✔
84
                        return cachedInstance;
1✔
85

86
                const credentials = await this.getCredentials(accountId);
8✔
87
                const lambdaInstance = new LambdaWrapper({ credentials });
6✔
88

89
                this.setCacheInstance(accountId, credentials, lambdaInstance);
6✔
90

91
                return lambdaInstance;
6✔
92
        }
93

94
        /**
95
         * @private
96
         * @static
97
         * @param {string} accountId JANIS Service Account ID
98
         * @returns {object} AWS Role credentials
99
         */
100
        static async getCredentials(accountId) {
101

102
                const roleArn = `arn:aws:iam::${accountId}:role/${this.roleName}`;
8✔
103

104
                let assumedRole;
105

106
                try {
8✔
107

108
                        assumedRole = await this.sts.assumeRole({
8✔
109
                                RoleArn: roleArn,
110
                                RoleSessionName: this.roleSessionName,
111
                                DurationSeconds: this.roleSessionDuration
112
                        });
113

114
                } catch(err) {
115
                        logger.warn(`Error while trying to assume role ${roleArn}: ${err.message}`);
1✔
116
                        assumedRole = false;
1✔
117
                }
118

119
                if(!assumedRole)
8✔
120
                        throw new LambdaError('Could not assume role for external service Lambda invocation', LambdaError.codes.ASSUME_ROLE_ERROR);
2✔
121

122
                const { Credentials } = assumedRole;
6✔
123

124
                return {
6✔
125
                        accessKeyId: Credentials.AccessKeyId,
126
                        secretAccessKey: Credentials.SecretAccessKey,
127
                        sessionToken: Credentials.SessionToken,
128
                        expiration: Credentials.Expiration
129
                };
130
        }
131

132
        /**
133
         * @private
134
         * @static
135
         * @param {string} accountId JANIS Service Account ID
136
         * @returns {Lambda} Lambda instance with role credentials
137
         */
138
        static getCachedInstance(accountId) {
139

140
                if(!this.cachedInstances || !this.cachedInstances[accountId])
9✔
141
                        return;
7✔
142

143
                const { credentials, lambdaInstance } = this.cachedInstances[accountId];
2✔
144

145
                return this.validateCredentials(credentials) && lambdaInstance;
2✔
146
        }
147

148
        /**
149
         * @private
150
         * @static
151
         * @param {string} accountId JANIS Service Account ID
152
         * @param {object} credentials AWS Role credentials
153
         * @param {Lambda} lambdaInstance Lambda instance with role credentials
154
        */
155
        static setCacheInstance(accountId, credentials, lambdaInstance) {
156

157
                if(!this.cachedInstances)
6✔
158
                        this.cachedInstances = {};
4✔
159

160
                this.cachedInstances[accountId] = { credentials, lambdaInstance };
6✔
161
        }
162

163
        /**
164
         * @private
165
         * @static
166
         * @param {object} credentials AWS Role credentials
167
         * @returns {boolean} true if credentials aren't expired, false otherwise
168
         */
169
        static validateCredentials(credentials) {
170
                return credentials && new Date(credentials.expiration) > new Date();
2✔
171
        }
172

173
        /**
174
         * @private
175
         * @static
176
         * @param {number} port Local Janis Service Port
177
         */
178
        static getInstanceForLocalService(port, code) {
179
                return new LambdaWrapper({ endpoint: this.buildLocalEndpoint(port, code) });
3✔
180
        }
181
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc