blob: ea7b7364c9f8c36df223c9258b47ca4843eac9ea [file] [log] [blame]
Sai Gandhamd67a9de2018-05-25 15:48:11 +00001.. This work is licensed under a Creative Commons Attribution 4.0 International License.
2.. http://creativecommons.org/licenses/by/4.0
Instrumental7a1817b2018-11-05 11:11:15 -06003.. Copyright © 2017 AT&T Intellectual Property. All rights reserved.
Sai Gandhamd67a9de2018-05-25 15:48:11 +00004
Instrumentalbbe71542018-05-25 12:29:38 -05005Client Configuration
6====================
Sai Gandhamd67a9de2018-05-25 15:48:11 +00007
Sai Gandhambd61eba2018-06-13 18:24:21 +00008TEST version of "cadi.properties"
9---------------------------------
10These properties point you to the ONAP TEST environment.
11
12Properties are separated into
13
14 * etc
15 * main Property file which provides Client specific info. As a client, this could be put in container, or placed on Host Box
16 * The important thing is to LINK the property with Location and Certificate Properties, see "local"
17 * local
18 * where there is Machine specific information (i.e. GEO Location (Latitude/Longitude)
19 * where this is Machine specific Certificates (for running services)
Instrumentalf3f006c2018-11-15 10:21:17 -060020
21 * This is because the certificates used must match the Endpoint that the Container is running on
22 * Note Certificate Manager can Place all these components together in one place.
23
24 * For April, 2018, please write Jonathan.gathman@att.com for credentials until TEST Env with Certificate Manager is fully tested. Include
25 1. AAF Namespace (you MUST be the owner for the request to be accepted)
26 2. Fully Qualified App ID (ID + Namespace)
27 3. Machine to be deployed on.
Sai Gandhambd61eba2018-06-13 18:24:21 +000028
29Client Credentials
30------------------
31For Beijing, full TLS is expected among all components. AAF provides the "Certificate Manager" which can "Place" Certificate information
32
33Example Source Code
34-------------------
Sai Gandham4e6f7bc2018-06-15 08:37:30 -050035Note the FULL class is available in the authz repo, cadi_aaf/org/onap/aaf/client/sample/Sample.java
36
37.. code-block:: java
38
39
40 /**
41 * ============LICENSE_START====================================================
42 * org.onap.aaf
43 * ===========================================================================
44 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
45 * ===========================================================================
46 * Licensed under the Apache License, Version 2.0 (the "License");
47 * you may not use this file except in compliance with the License.
48 * You may obtain a copy of the License at
49 *
50 * http://www.apache.org/licenses/LICENSE-2.0
51 *
52 * Unless required by applicable law or agreed to in writing, software
53 * distributed under the License is distributed on an "AS IS" BASIS,
54 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
55 * See the License for the specific language governing permissions and
56 * limitations under the License.
57 * ============LICENSE_END====================================================
58 *
59 */
60
61 package org.onap.aaf.client.sample;
62
63 import java.io.IOException;
64 import java.security.Principal;
65 import java.util.ArrayList;
66 import java.util.List;
67
68 import org.onap.aaf.cadi.Access;
69 import org.onap.aaf.cadi.CadiException;
70 import org.onap.aaf.cadi.LocatorException;
71 import org.onap.aaf.cadi.Permission;
72 import org.onap.aaf.cadi.PropAccess;
73 import org.onap.aaf.cadi.aaf.AAFPermission;
74 import org.onap.aaf.cadi.aaf.v2_0.AAFAuthn;
75 import org.onap.aaf.cadi.aaf.v2_0.AAFConHttp;
76 import org.onap.aaf.cadi.aaf.v2_0.AAFLurPerm;
77 import org.onap.aaf.cadi.principal.UnAuthPrincipal;
78 import org.onap.aaf.cadi.util.Split;
79 import org.onap.aaf.misc.env.APIException;
80
81 public class Sample {
82 private static Sample singleton;
83 final private AAFConHttp aafcon;
84 final private AAFLurPerm aafLur;
85 final private AAFAuthn<?> aafAuthn;
86
87 /**
88 * This method is to emphasize the importance of not creating the AAFObjects over and over again.
89 * @return
90 */
91 public static Sample singleton() {
92 return singleton;
93 }
94
95 public Sample(Access myAccess) throws APIException, CadiException, LocatorException {
96 aafcon = new AAFConHttp(myAccess);
97 aafLur = aafcon.newLur();
98 aafAuthn = aafcon.newAuthn(aafLur);
99 }
100
101 /**
102 * Checking credentials outside of HTTP/S presents fewer options initially. There is not, for instance,
103 * the option of using 2-way TLS HTTP/S.
104 *
105 * However, Password Checks are still useful, and, if the Client Certificate could be obtained in other ways, the
106 * Interface can be expanded in the future to include Certificates.
107 * @throws CadiException
108 * @throws IOException
109 */
110 public Principal checkUserPass(String fqi, String pass) throws IOException, CadiException {
111 String ok = aafAuthn.validate(fqi, pass);
112 if(ok==null) {
113 System.out.println("Success!");
114 /*
115 UnAuthPrincipal means that it is not coming from the official Authorization chain.
116 This is useful for Security Plugins which don't use Principal as the tie between
117 Authentication and Authorization
118
119 You can also use this if you want to check Authorization without actually Authenticating, as may
120 be the case with certain Onboarding Tooling.
121 */
122 return new UnAuthPrincipal(fqi);
123 } else {
124 System.out.printf("Failure: %s\n",ok);
125 return null;
126 }
127
128
129 }
130
131 /**
132 * An example of looking for One Permission within all the permissions user has. CADI does cache these,
133 * so the call is not expensive.
134 *
135 * Note: If you are using "J2EE" (Servlets), CADI ties this function to the method:
136 * HttpServletRequest.isUserInRole(String user)
137 *
138 * The J2EE user can expect that his servlet will NOT be called without a Validated Principal, and that
139 * "isUserInRole()" will validate if the user has the Permission designated.
140 *
141 */
142 public boolean oneAuthorization(Principal fqi, Permission p) {
143 return aafLur.fish(fqi, p);
144 }
145
146 public List<Permission> allAuthorization(Principal fqi) {
147 List<Permission> pond = new ArrayList<Permission>();
148 aafLur.fishAll(fqi, pond);
149 return pond;
150 }
151
152
153 public static void main(String[] args) {
154 // Note: you can pick up Properties from Command line as well as VM Properties
155 // Code "user_fqi=... user_pass=..." (where user_pass can be encrypted) in the command line for this sample.
156 // Also code "perm=<perm type>|<instance>|<action>" to test a specific Permission
157 PropAccess myAccess = new PropAccess(args);
158 try {
159 /*
160 * NOTE: Do NOT CREATE new aafcon, aafLur and aafAuthn each transaction. They are built to be
161 * reused!
162 *
163 * This is why this code demonstrates "Sample" as a singleton.
164 */
165 singleton = new Sample(myAccess);
166 String user = myAccess.getProperty("user_fqi");
167 String pass= myAccess.getProperty("user_pass");
168
169 if(user==null || pass==null) {
170 System.err.println("This Sample class requires properties user_fqi and user_pass");
171 } else {
172 pass = myAccess.decrypt(pass, false); // Note, with "false", decryption will only happen if starts with "enc:"
173 // See the CODE for Java Methods used
174 Principal fqi = Sample.singleton().checkUserPass(user,pass);
175
176 if(fqi==null) {
177 System.out.println("OK, normally, you would cease processing for an "
178 + "unauthenticated user, but for the purpose of Sample, we'll keep going.\n");
179 fqi=new UnAuthPrincipal(user);
180 }
181
182 // AGAIN, NOTE: If your client fails Authentication, the right behavior 99.9%
183 // of the time is to drop the transaction. We continue for sample only.
184
185 // note, default String for perm
186 String permS = myAccess.getProperty("perm","org.osaaf.aaf.access|*|read");
187 String[] permA = Split.splitTrim('|', permS);
188 if(permA.length>2) {
189 final Permission perm = new AAFPermission(permA[0],permA[1],permA[2]);
190 // See the CODE for Java Methods used
191 if(singleton().oneAuthorization(fqi, perm)) {
192 System.out.printf("Success: %s has %s\n",fqi.getName(),permS);
193 } else {
194 System.out.printf("%s does NOT have %s\n",fqi.getName(),permS);
195 }
196 }
197
198
199 // Another form, you can get ALL permissions in a list
200 // See the CODE for Java Methods used
201 List<Permission> permL = singleton().allAuthorization(fqi);
202 if(permL.size()==0) {
203 System.out.printf("User %s has no Permissions THAT THE CALLER CAN SEE",fqi.getName());
204 } else {
205 System.out.print("Success:\n");
206 for(Permission p : permL) {
207 System.out.printf("\t%s has %s\n",fqi.getName(),p.getKey());
208 }
209 }
210 }
211 } catch (APIException | CadiException | LocatorException | IOException e) {
212 e.printStackTrace();
213 }
214 }
Instrumentalf3f006c2018-11-15 10:21:17 -0600215 }
216