Basic auth login and logout command
[cli.git] / framework / src / main / java / org / onap / cli / fw / ad / OnapAuthClient.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
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 org.onap.cli.fw.ad;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.http.HttpStatus;
24 import org.apache.http.auth.UsernamePasswordCredentials;
25 import org.apache.http.impl.auth.BasicScheme;
26 import org.onap.cli.fw.OnapCommand;
27 import org.onap.cli.fw.OnapCommandRegistrar;
28 import org.onap.cli.fw.conf.Constants;
29 import org.onap.cli.fw.conf.OnapCommandConfg;
30 import org.onap.cli.fw.error.OnapCommandException;
31 import org.onap.cli.fw.error.OnapCommandExecutionFailed;
32 import org.onap.cli.fw.error.OnapCommandHttpFailure;
33 import org.onap.cli.fw.error.OnapCommandInvalidParameterValue;
34 import org.onap.cli.fw.error.OnapCommandLoginFailed;
35 import org.onap.cli.fw.error.OnapCommandLogoutFailed;
36 import org.onap.cli.fw.error.OnapCommandNotFound;
37 import org.onap.cli.fw.error.OnapCommandServiceNotFound;
38 import org.onap.cli.fw.http.HttpInput;
39 import org.onap.cli.fw.http.HttpResult;
40 import org.onap.cli.fw.http.OnapHttpConnection;
41 import org.onap.cli.fw.input.OnapCommandParameter;
42 import org.onap.cli.fw.output.OnapCommandResultAttribute;
43 import org.onap.cli.fw.utils.OnapCommandUtils;
44
45 import com.jayway.jsonpath.JsonPath;
46
47 /**
48  * Onap Auth client helps to do login and logout.
49  *
50  */
51 public class OnapAuthClient {
52
53         private OnapCommand cmd = null;
54         
55     private OnapHttpConnection http = null;
56
57     public OnapAuthClient(OnapCommand cmd, boolean debug) throws OnapCommandHttpFailure, OnapCommandInvalidParameterValue {
58         this.cmd = cmd;
59         this.http = new OnapHttpConnection(debug);
60     }
61
62     /**
63      * Login.
64      *
65      * @throws OnapCommandException
66      *             exception
67      */
68     public void login() throws OnapCommandException {
69
70         // For development purpose, its introduced and is not supported for production
71         if (OnapCommandConfg.isAuthIgnored()) {
72             return;
73         }
74
75         OnapCommand login = this.findAuthCommand("login");        
76         
77         OnapCommandUtils.copyParamsFrom(this.cmd, login);
78         login.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).setValue(this.getServiceUrl(login));
79         login.execute();
80         
81         //It is safely assumed that all outputs are considered as common http headers.
82         Map<String, String> headers = new HashMap<>();
83         for (OnapCommandResultAttribute    attr: login.getResult().getRecords()) {
84             String headerValue = attr.getValues().get(0);
85             if (headerValue != null && !headerValue.isEmpty()) {
86                 headers.put(attr.getName(), attr.getValues().get(0));
87             }
88         }
89         
90         this.http.setCommonHeaders(headers);
91     }
92
93     /**
94      * Logout.
95      *
96      * @throws OnapCommandException
97      *             exception
98      */
99     public void logout() throws OnapCommandException {
100         // For development purpose, its introduced and is not supported for production
101         if (OnapCommandConfg.isAuthIgnored()) {
102             return;
103         }
104
105         OnapCommand logout = this.findAuthCommand("logout");
106         
107         OnapCommandUtils.copyParamsFrom(this.cmd, logout);
108         
109         logout.execute();
110         
111         this.http.close();
112     }
113
114     /**
115      * Find given service base path.
116      *
117      * @throws OnapCommandException
118      *             exception
119      */
120     public String getServiceUrl() throws OnapCommandException {
121         return this.getServiceUrl(this.cmd);
122     }
123
124     private String getServiceUrl(OnapCommand cmd) throws OnapCommandException {
125         if (cmd.getService().isModeDirect()){
126                 return cmd.getParametersMap().get(Constants.DEAFULT_PARAMETER_HOST_URL).getValue().toString();
127         } else { //Catalog mode
128             OnapCommand catalog = OnapCommandRegistrar.getRegistrar().get("catalog");
129             
130             OnapCommandUtils.copyParamsFrom(cmd, catalog);
131             
132             catalog.execute();
133             
134             String hostUrl = catalog.getResult().getRecordsMap().get(Constants.CATALOG_SERVICE_HOST_URL).getValues().get(0);
135             hostUrl = hostUrl.trim();
136             if (hostUrl.endsWith("/")) {
137                 hostUrl = hostUrl.substring(0, hostUrl.length()-1);
138             }
139             
140             String basePath = catalog.getResult().getRecordsMap().get(Constants.CATALOG_SERVICE_BASE_PATH).getValues().get(0);
141             basePath = basePath.trim();
142             if (basePath.startsWith("/")) {
143                 basePath = basePath.substring(1);
144             }
145             
146             return hostUrl + "/" + basePath;
147         }
148     }
149
150
151     public String getDebugInfo() {
152         return this.http.getDebugInfo();
153     }
154
155     /**
156      * Http call to external service.
157      *
158      * @param input
159      *            http input
160      * @return http result
161      * @throws OnapCommandHttpFailure
162      *             exception
163      */
164     public HttpResult run(HttpInput input) throws OnapCommandHttpFailure {
165         return this.http.request(input);
166     }
167     
168     private OnapCommand findAuthCommand(String authAction) throws OnapCommandException {
169         OnapCommand auth = null;
170         try {
171                 //Find the auth command for the given service and version under current enabled product
172                 auth = OnapCommandRegistrar.getRegistrar().get(
173                                 this.cmd.getService().getName() + "-" +
174                                 this.cmd.getService().getVersion() + "-" +
175                                 this.cmd.getService().getAuthType() + "-" + authAction);
176         } catch (OnapCommandNotFound e) {
177                 try {
178                         //Find the auth command for the given service under current enabled product
179                         auth = OnapCommandRegistrar.getRegistrar().get(
180                                 this.cmd.getService().getName() + "-" +
181                                 this.cmd.getService().getAuthType() + "-" + authAction);
182             } catch (OnapCommandNotFound e1) {
183                         //Find the auth command for current enabled product
184                 auth = OnapCommandRegistrar.getRegistrar().get(
185                                         this.cmd.getService().getAuthType() + "-" + authAction);
186             }
187         }
188         
189         return auth;
190     }
191 }