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