LMCL 1.4.4 - Nexus auth support
[federation.git] / gateway / src / main / java / org / acumos / federation / gateway / Clients.java
1 /*-
2  * ===============LICENSE_START=======================================================
3  * Acumos
4  * ===================================================================================
5  * Copyright (C) 2017-2020 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6  * ===================================================================================
7  * This Acumos software file is distributed by AT&T and Tech Mahindra
8  * under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * This file is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ===============LICENSE_END=========================================================
19  */
20 package org.acumos.federation.gateway;
21
22 import org.springframework.beans.factory.annotation.Autowired;
23
24 import com.github.dockerjava.api.DockerClient;
25 import com.github.dockerjava.core.DefaultDockerClientConfig;
26 import com.github.dockerjava.core.DockerClientBuilder;
27
28 import org.acumos.cds.client.ICommonDataServiceRestClient;
29 import org.acumos.cds.client.CommonDataServiceRestClientImpl;
30
31 import org.acumos.federation.client.config.ClientConfig;
32 import org.acumos.federation.client.ClientBase;
33 import org.acumos.federation.client.FederationClient;
34
35 import org.acumos.securityverification.service.ISecurityVerificationClientService;
36 import org.acumos.securityverification.service.SecurityVerificationClientServiceImpl;
37 import org.acumos.licensemanager.client.rtu.LicenseAsset;
38 import org.acumos.nexus.client.NexusArtifactClient;
39 import org.acumos.nexus.client.RepositoryLocation;
40
41 /**
42  * Defines all beans used to access outside services.
43  *
44  * By mocking this bean, all external access can be stubbed out.
45  */
46 public class Clients {
47         /*
48          * Implementation note:
49          *
50          * Ideally, all clients would be created at startup, and the getXXX()
51          * methods would just return them, however, while the Spring framework
52          * guarantees that @Autowired fields have been populated, before
53          * invoking @PostConstruct annotated methods and the afterPropertiesSet
54          * method, it doesn't guarantee that properties in those beans have
55          * been set, and the outcome is unrelable.  @Lazy could have been used,
56          * but it would need to be set both here, and in all the @Autowired
57          * uses: missing a single one, in future changes, would produce
58          * mysterious unrelable results.  So, instead, this code
59          * creates clients on first use and, where possible, keeps them for
60          * future use.
61          */
62
63         @Autowired
64         private FederationConfig federation;
65
66         @Autowired
67         private ServiceConfig cdmsConfig;
68
69         @Autowired
70         private NexusConfig nexusConfig;
71
72         @Autowired
73         private DockerConfig dockerConfig;
74
75         @Autowired
76         private ServiceConfig verificationConfig;
77
78         @Autowired
79         private ServiceConfig lmConfig;
80
81         @Autowired
82         private ServiceConfig logstashConfig;
83
84         private ICommonDataServiceRestClient cdsClient;
85         private NexusClient nexusClient;
86         private ISecurityVerificationClientService svClient;
87         private LicenseAsset lmClient;
88         private LogstashClient logstashClient;
89
90         public FederationClient getFederationClient(String url) {
91                 /*
92                  * The set of peers can change, at runtime, and there is no
93                  * notification when one is deleted (or has its API URL
94                  * changed).  It would have been possible to keep federation
95                  * clients in a hash and fault them in, as needed, but, without
96                  * a means for identifying clients that were no longer needed,
97                  * that would have constituted a memory (and possibly a TCP/IP
98                  * connection) leak.  So this code does not cache federation
99                  * clients.
100                  */
101                 return new FederationClient(url, federation);
102         }
103
104         public synchronized ICommonDataServiceRestClient getCDSClient() {
105                 if (cdsClient == null) {
106                         String url = cdmsConfig.getUrl();
107                         ClientConfig cc = new ClientConfig();
108                         cc.setCreds(cdmsConfig);
109                         cdsClient = CommonDataServiceRestClientImpl.getInstance(url, ClientBase.buildRestTemplate(url, cc, null, null));
110                 }
111                 return cdsClient;
112         }
113
114         public synchronized NexusClient getNexusClient() {
115                 if (nexusClient == null) {
116                         ClientConfig cc = new ClientConfig();
117                         cc.setCreds(nexusConfig);
118                         nexusClient = new NexusClient(nexusConfig.getUrl(), cc);
119                 }
120                 return nexusClient;
121         }
122
123         public synchronized DockerClient getDockerClient() {
124                 /*
125                  * For some reason, the DockerClient seems to go stale,
126                  * resulting in operations (like the docker pull command)
127                  * unexpectedly hanging, with no error or indication of a
128                  * problem.  Creating a fresh DockerClient on each
129                  * upload/download of a Docker image artifact, as a
130                  * workaround, seems to work.
131                  */
132                 return DockerClientBuilder.getInstance(
133                     DefaultDockerClientConfig.createDefaultConfigBuilder()
134                         .withDockerHost(dockerConfig.getHost())
135                         .withDockerTlsVerify(dockerConfig.getTlsVerify())
136                         .withDockerConfig(dockerConfig.getDockerConfig())
137                         .withDockerCertPath(dockerConfig.getDockerCertPath())
138                         .withApiVersion(dockerConfig.getApiVersion())
139                         .withRegistryUsername(dockerConfig.getRegistryUsername())
140                         .withRegistryPassword(dockerConfig.getRegistryPassword())
141                         .withRegistryEmail(dockerConfig.getRegistryEmail())
142                         .withRegistryUrl(dockerConfig.getRegistryUrl())
143                         .build()
144                     ).build();
145         }
146
147         public synchronized ISecurityVerificationClientService getSVClient() {
148                 if (svClient == null) {
149                         svClient = new SecurityVerificationClientServiceImpl(
150                             verificationConfig.getUrl(),
151                             cdmsConfig.getUrl(),
152                             cdmsConfig.getUsername(),
153                             cdmsConfig.getPassword(),
154                             nexusConfig.getUrl().replaceAll("/*$", "") + "/",
155                             nexusConfig.getUsername(),
156                             nexusConfig.getPassword());
157                 }
158                 return svClient;
159         }
160
161         public synchronized LicenseAsset getLMClient() {
162                 if (lmClient == null) {
163                         RepositoryLocation repositoryLocation = new RepositoryLocation();
164                         repositoryLocation.setId("1");
165                         repositoryLocation.setUrl(nexusConfig.getUrl().replaceAll("/*$", "") + "/");
166                         repositoryLocation.setUsername(nexusConfig.getUsername());
167                         repositoryLocation.setPassword(nexusConfig.getPassword());
168                         lmClient = new LicenseAsset(getCDSClient(), lmConfig.getUrl(), new NexusArtifactClient(repositoryLocation));
169                 }
170                 return lmClient;
171         }
172
173         public synchronized LogstashClient getLogstashClient(){
174                 if(logstashClient == null){
175                         ClientConfig cc = new ClientConfig();
176                         cc.setCreds(logstashConfig);
177                         logstashClient = new LogstashClient(logstashConfig.getUrl(), cc);
178                 }
179                 return logstashClient;
180         }
181 }