|
1 | 1 | package com.venafi.vcert.sdk; |
2 | 2 |
|
3 | | -import com.venafi.vcert.sdk.endpoint.Authentication; |
4 | | -import com.venafi.vcert.sdk.endpoint.ConnectorType; |
5 | | -import lombok.Builder; |
6 | | -import lombok.Data; |
7 | | -import org.ini4j.Profile; |
8 | | -import org.ini4j.Wini; |
9 | | - |
| 3 | +import static java.util.Arrays.asList; |
10 | 4 | import java.io.IOException; |
11 | 5 | import java.nio.file.Path; |
12 | 6 | import java.util.List; |
13 | 7 | import java.util.Objects; |
14 | | - |
15 | | -import static java.util.Arrays.asList; |
| 8 | +import org.ini4j.Profile; |
| 9 | +import org.ini4j.Wini; |
| 10 | +import lombok.Builder; |
| 11 | +import lombok.Data; |
| 12 | +import com.venafi.vcert.sdk.endpoint.Authentication; |
| 13 | +import com.venafi.vcert.sdk.endpoint.ConnectorType; |
16 | 14 |
|
17 | 15 | @Data |
18 | 16 | @Builder |
19 | 17 | public class Config { |
20 | | - public static final String DEFAULT_SECTION = "?"; |
21 | | - public static final List<String> VALID_TPP_KEYS = |
22 | | - asList("tpp_url", "tpp_user", "tpp_password", "tpp_zone", "trust_bundle"); |
| 18 | + public static final String DEFAULT_SECTION = "?"; |
| 19 | + public static final List<String> VALID_TPP_KEYS = |
| 20 | + asList("tpp_url", "tpp_user", "tpp_password", "tpp_zone", "trust_bundle"); |
23 | 21 |
|
24 | | - public static final List<String> VALID_CLOUD_KEYS = |
25 | | - asList("cloud_url", "cloud_apikey", "cloud_zone", "trust_bundle"); |
| 22 | + public static final List<String> VALID_CLOUD_KEYS = |
| 23 | + asList("cloud_url", "cloud_apikey", "cloud_zone", "trust_bundle"); |
26 | 24 |
|
27 | | - private ConnectorType connectorType; |
28 | | - private String baseUrl; |
29 | | - private String zone; |
30 | | - private Authentication credentials; |
31 | | - private String connectionTrust; |
32 | | - private boolean logVerbose; |
33 | | - private String configFile; |
34 | | - private String configSection; |
| 25 | + private ConnectorType connectorType; |
| 26 | + private String baseUrl; |
| 27 | + private String zone; |
| 28 | + private Authentication credentials; |
| 29 | + private String connectionTrust; |
| 30 | + private boolean logVerbose; |
| 31 | + private String configFile; |
| 32 | + private String configSection; |
35 | 33 |
|
36 | 34 |
|
37 | | - public static Config loadConfigFromFile(Path path) throws VCertException { |
38 | | - final ConfigBuilder builder = Config.builder(); |
39 | | - final Authentication.AuthenticationBuilder authBuilder = Authentication.builder(); |
| 35 | + public static Config loadConfigFromFile(Path path) throws VCertException { |
| 36 | + final ConfigBuilder builder = Config.builder(); |
| 37 | + final Authentication.AuthenticationBuilder authBuilder = Authentication.builder(); |
40 | 38 |
|
41 | | - try { |
42 | | - final Wini ini = new Wini(path.toFile()); |
43 | | - final Profile.Section defaultSection = ini.get(DEFAULT_SECTION); |
44 | | - validateConfigFile(defaultSection); |
| 39 | + try { |
| 40 | + final Wini ini = new Wini(path.toFile()); |
| 41 | + final Profile.Section defaultSection = ini.get(DEFAULT_SECTION); |
| 42 | + validateConfigFile(defaultSection); |
45 | 43 |
|
46 | | - if (defaultSection.containsKey("tpp_url")) { |
47 | | - builder.connectorType(ConnectorType.TPP); |
48 | | - builder.baseUrl(defaultSection.get("tpp_url")); |
49 | | - authBuilder.user(defaultSection.get("tpp_user")); |
50 | | - authBuilder.password(defaultSection.get("tpp_password")); |
| 44 | + if (defaultSection.containsKey("tpp_url")) { |
| 45 | + builder.connectorType(ConnectorType.TPP); |
| 46 | + builder.baseUrl(defaultSection.get("tpp_url")); |
| 47 | + authBuilder.user(defaultSection.get("tpp_user")); |
| 48 | + authBuilder.password(defaultSection.get("tpp_password")); |
51 | 49 |
|
52 | | - if (defaultSection.containsKey("tpp_zone")) { |
53 | | - builder.zone(defaultSection.get("tpp_zone")); |
54 | | - } |
55 | | - } else if (defaultSection.containsKey("cloud_apikey")) { |
56 | | - authBuilder.apiKey(defaultSection.get("cloud_apikey")); |
57 | | - if (defaultSection.containsKey("cloud_url")) { |
58 | | - builder.baseUrl(defaultSection.get("cloud_url")); |
59 | | - } |
60 | | - if (defaultSection.containsKey("cloud_zone")) { |
61 | | - builder.zone(defaultSection.get("cloud_zone")); |
62 | | - } |
63 | | - } |
64 | | - builder.credentials(authBuilder.build()); |
65 | | - return builder.build(); |
66 | | - } catch (IOException e) { |
67 | | - throw new VCertException(String.format("Access error to the configuration file: %s", path.toString())); |
| 50 | + if (defaultSection.containsKey("tpp_zone")) { |
| 51 | + builder.zone(defaultSection.get("tpp_zone")); |
68 | 52 | } |
| 53 | + } else if (defaultSection.containsKey("cloud_apikey")) { |
| 54 | + authBuilder.apiKey(defaultSection.get("cloud_apikey")); |
| 55 | + if (defaultSection.containsKey("cloud_url")) { |
| 56 | + builder.baseUrl(defaultSection.get("cloud_url")); |
| 57 | + } |
| 58 | + if (defaultSection.containsKey("cloud_zone")) { |
| 59 | + builder.zone(defaultSection.get("cloud_zone")); |
| 60 | + } |
| 61 | + } |
| 62 | + builder.credentials(authBuilder.build()); |
| 63 | + return builder.build(); |
| 64 | + } catch (IOException e) { |
| 65 | + throw new VCertException( |
| 66 | + String.format("Access error to the configuration file: %s", path.toString())); |
69 | 67 | } |
| 68 | + } |
70 | 69 |
|
71 | | - private static void validateConfigFile(Profile.Section defaultSection) throws VCertException { |
72 | | - if (Objects.isNull(defaultSection)) { |
73 | | - throw new VCertException("The configuration file is empty"); |
74 | | - } |
| 70 | + private static void validateConfigFile(Profile.Section defaultSection) throws VCertException { |
| 71 | + if (Objects.isNull(defaultSection)) { |
| 72 | + throw new VCertException("The configuration file is empty"); |
| 73 | + } |
75 | 74 |
|
76 | | - if (defaultSection.containsKey("tpp_url")) { |
77 | | - for (String key : defaultSection.keySet()) { |
78 | | - if (!VALID_TPP_KEYS.contains(key)) { |
79 | | - throw new VCertException(String.format("illegal key %s in section %s", key, defaultSection.getName())); |
80 | | - } |
81 | | - } |
82 | | - if (!defaultSection.containsKey("tpp_user")) { |
83 | | - throw new VCertException(String.format("configuration issue section %s: missing TTP user", |
84 | | - defaultSection.getName())); |
85 | | - } |
86 | | - if (!defaultSection.containsKey("tpp_password")) { |
87 | | - throw new VCertException(String.format("configuration issue section %s: missing TTP password", |
88 | | - defaultSection.getName())); |
89 | | - } |
90 | | - } else if (defaultSection.containsKey("cloud_apikey")){ |
91 | | - for (String key : defaultSection.keySet()) { |
92 | | - if (!VALID_CLOUD_KEYS.contains(key)) { |
93 | | - throw new VCertException(String.format("illegal key %s in section %s", key, defaultSection.getName())); |
94 | | - } |
95 | | - } |
96 | | - } else { |
97 | | - throw new VCertException(String.format("Section %s requires 'tpp_url' or 'cloud_apikey'", defaultSection.getName())); |
| 75 | + if (defaultSection.containsKey("tpp_url")) { |
| 76 | + for (String key : defaultSection.keySet()) { |
| 77 | + if (!VALID_TPP_KEYS.contains(key)) { |
| 78 | + throw new VCertException( |
| 79 | + String.format("illegal key %s in section %s", key, defaultSection.getName())); |
| 80 | + } |
| 81 | + } |
| 82 | + if (!defaultSection.containsKey("tpp_user")) { |
| 83 | + throw new VCertException(String.format("configuration issue section %s: missing TPP user", |
| 84 | + defaultSection.getName())); |
| 85 | + } |
| 86 | + if (!defaultSection.containsKey("tpp_password")) { |
| 87 | + throw new VCertException(String.format( |
| 88 | + "configuration issue section %s: missing TPP password", defaultSection.getName())); |
| 89 | + } |
| 90 | + } else if (defaultSection.containsKey("cloud_apikey")) { |
| 91 | + for (String key : defaultSection.keySet()) { |
| 92 | + if (!VALID_CLOUD_KEYS.contains(key)) { |
| 93 | + throw new VCertException( |
| 94 | + String.format("illegal key %s in section %s", key, defaultSection.getName())); |
98 | 95 | } |
| 96 | + } |
| 97 | + } else { |
| 98 | + throw new VCertException(String.format("Section %s requires 'tpp_url' or 'cloud_apikey'", |
| 99 | + defaultSection.getName())); |
99 | 100 | } |
| 101 | + } |
100 | 102 |
|
101 | 103 | } |
0 commit comments