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

ebourg / jsign / #368

22 Mar 2025 10:03PM UTC coverage: 83.31% (-0.2%) from 83.503%
#368

push

ebourg
Changelog & credits update

4862 of 5836 relevant lines covered (83.31%)

0.83 hits per line

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

93.55
/jsign-crypto/src/main/java/net/jsign/jca/AmazonCredentials.java
1
/*
2
 * Copyright 2023 Emmanuel Bourg
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package net.jsign.jca;
18

19
import java.io.IOException;
20

21
/**
22
 * AWS credentials
23
 *
24
 * @since 5.0
25
 */
26
public class AmazonCredentials {
27

28
    private final String accessKey;
29
    private final String secretKey;
30
    private final String sessionToken;
31

32
    public AmazonCredentials(String accessKey, String secretKey, String sessionToken) {
1✔
33
        this.accessKey = accessKey;
1✔
34
        this.secretKey = secretKey;
1✔
35
        this.sessionToken = sessionToken;
1✔
36
    }
1✔
37

38
    public String getAccessKey() {
39
        return accessKey;
1✔
40
    }
41

42
    public String getSecretKey() {
43
        return secretKey;
1✔
44
    }
45

46
    public String getSessionToken() {
47
        return sessionToken;
1✔
48
    }
49

50
    /**
51
     * Parses the concatenated AWS credentials
52
     *
53
     * @param credentials <tt>accessKey|secretKey|sessionToken</tt> (the session token is optional)
54
     */
55
    public static AmazonCredentials parse(String credentials) throws IllegalArgumentException {
56
        // parse the credentials
57
        String[] elements = credentials.split("\\|", 3);
1✔
58
        if (elements.length < 2) {
1✔
59
            throw new IllegalArgumentException("Invalid AWS credentials: " + credentials);
1✔
60
        }
61
        String accessKey = elements[0];
1✔
62
        String secretKey = elements[1];
1✔
63
        String sessionToken = elements.length > 2 ? elements[2] : null;
1✔
64

65
        return new AmazonCredentials(accessKey, secretKey, sessionToken);   
1✔
66
    }
67

68
    /**
69
     * Returns the default AWS credentials, fetched from the following sources in order:
70
     * <ul>
71
     *   <li>The environment variables <tt>AWS_ACCESS_KEY_ID</tt> (or <tt>AWS_ACCESS_KEY</tt>),
72
     *       <tt>AWS_SECRET_KEY</tt> (or <tt>AWS_SECRET_ACCESS_KEY</tt>) and <tt>AWS_SESSION_TOKEN</tt></li>
73
     *   <li>The ECS container credentials service (ECS, EKS, Greengrass, Fargate)</li>
74
     *   <li>The EC2 instance metadata service (IMDSv2)</li>
75
     * </ul>
76
     */
77
    public static AmazonCredentials getDefault() throws IOException {
78
        if (getenv("AWS_ACCESS_KEY_ID") != null || getenv("AWS_ACCESS_KEY") != null) {
1✔
79
            String accessKey = getenv("AWS_ACCESS_KEY_ID");
1✔
80
            if (accessKey == null) {
1✔
81
                accessKey = getenv("AWS_ACCESS_KEY");
1✔
82
            }
83
            String secretKey = getenv("AWS_SECRET_KEY");
1✔
84
            if (secretKey == null) {
1✔
85
                secretKey = getenv("AWS_SECRET_ACCESS_KEY");
1✔
86
            }
87
            String sessionToken = getenv("AWS_SESSION_TOKEN");
1✔
88

89
            return new AmazonCredentials(accessKey, secretKey, sessionToken);
1✔
90
        } else {
91
            try {
92
                return new AmazonECSCredentialsClient().getCredentials();
×
93
            } catch (IOException ecsException) {
1✔
94
                try {
95
                    return new AmazonIMDS2Client().getCredentials();
×
96
                } catch (IOException imds2Exception) {
1✔
97
                    ecsException.addSuppressed(imds2Exception);
1✔
98
                    throw ecsException;
1✔
99
                }
100
            }
101
        }
102
    }
103

104
    static String getenv(String name) {
105
        return System.getenv(name);
1✔
106
    }
107
}
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

© 2026 Coveralls, Inc