summaryrefslogtreecommitdiffstats
path: root/auth/auth-certman/src/main/java/org/onap/aaf/auth/cm/mapper/Mapper1_0.java
blob: 22243ae480995a71a4bf946e0616484b2e761ace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
 * ============LICENSE_START====================================================
 * org.onap.aaf
 * ===========================================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * ===========================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END====================================================
 *
 */

package org.onap.aaf.auth.cm.mapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.onap.aaf.auth.cm.data.CertDrop;
import org.onap.aaf.auth.cm.data.CertRenew;
import org.onap.aaf.auth.cm.data.CertReq;
import org.onap.aaf.auth.cm.data.CertResp;
import org.onap.aaf.auth.cm.validation.CertmanValidator;
import org.onap.aaf.auth.dao.cass.ArtiDAO;
import org.onap.aaf.auth.dao.cass.ArtiDAO.Data;
import org.onap.aaf.auth.dao.cass.CertDAO;
import org.onap.aaf.auth.env.AuthzTrans;
import org.onap.aaf.auth.layer.Result;
import org.onap.aaf.cadi.util.FQI;
import org.onap.aaf.cadi.util.Vars;

import aaf.v2_0.Error;
import certman.v1_0.Artifacts;
import certman.v1_0.Artifacts.Artifact;
import certman.v1_0.BaseRequest;
import certman.v1_0.CertInfo;
import certman.v1_0.CertificateDrop;
import certman.v1_0.CertificateRenew;
import certman.v1_0.CertificateRequest;


public class Mapper1_0 implements Mapper<BaseRequest,CertInfo,Artifacts,Error> {
    
    @Override
    public Class<?> getClass(API api) {
        switch(api) {
            case CERT_REQ: return CertificateRequest.class;
            case CERT_RENEW: return CertificateRenew.class;
            case CERT_DROP: return CertificateDrop.class;
            case CERT: return CertInfo.class;
            case ARTIFACTS: return Artifacts.class;
            case ERROR: return Error.class;
            case VOID: return Void.class;
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <A> A newInstance(API api) {
        switch(api) {
            case CERT_REQ: return (A) new CertificateRequest();
            case CERT_RENEW: return (A) new CertificateRenew();
            case CERT_DROP: return (A) new CertificateDrop();
            case CERT: return (A) new CertInfo();
            case ARTIFACTS: return (A) new Artifacts();
            case ERROR: return (A)new Error();
            case VOID: return null;
        }
        return null;
    }

    //////////////  Mapping Functions /////////////
    @Override
    public Error errorFromMessage(StringBuilder holder, String msgID, String text, Object ... var) {
        Error err = new Error();
        err.setMessageId(msgID);
        // AT&T Restful Error Format requires numbers "%" placements
        err.setText(Vars.convert(holder, text, var));
        for (Object s : var) {
            err.getVariables().add(s.toString());
        }
        return err;
    }

    /* (non-Javadoc)
     * @see com.att.authz.certman.mapper.Mapper#toCert(org.onap.aaf.auth.env.test.AuthzTrans, org.onap.aaf.auth.layer.test.Result)
     */
    @Override
    public Result<CertInfo> toCert(AuthzTrans trans, Result<CertResp> in, boolean withTrustChain) throws IOException {
        if (!in.isOK()) {
            return Result.err(in);
        }

        CertResp cin = in.value;
        CertInfo cout = newInstance(API.CERT);
        cout.setPrivatekey(cin.privateString());
        String value;
        if ((value=cin.challenge())!=null) {
            cout.setChallenge(value);
        }
        // In Version 1, Cert is always first
        cout.getCerts().add(cin.asCertString());
        // Follow with Trust Chain
        if (cin.trustChain()!=null) {
            for (String c : cin.trustChain()) {
                if (c!=null) {
                    cout.getCerts().add(c);
                }
            }
        }

        // Adding all the Certs in one response is a mistake.  Makes it very hard for Agent to setup
        // Certs in keystore versus Truststore.  Separate in Version 2_0
        if (cin.trustCAs()!=null) {
            for (String c : cin.trustCAs()) {
                if (c!=null) {
                    if (!cout.getCerts().contains(c)) {
                        cout.getCerts().add(c);
                    }
                }
            }
        }
        if (cin.notes()!=null) {
            boolean first = true;
            StringBuilder sb = new StringBuilder();
            for (String n : cin.notes()) {
                if (first) {
                    first = false;
                } else {
                    sb.append('\n');
                }
                sb.append(n);
            }
            cout.setNotes(sb.toString());
        }
        List<String> caIssuerDNs = cout.getCaIssuerDNs();
        for (String s : cin.caIssuerDNs()) {
            caIssuerDNs.add(s);
        }
        cout.setEnv(cin.env());
        return Result.ok(cout);

    }

    @Override
    public Result<CertInfo> toCert(AuthzTrans trans, Result<List<CertDAO.Data>> in) {
        if (in.isOK()) {
            CertInfo cout = newInstance(API.CERT);
            List<String> certs = cout.getCerts();
            for (CertDAO.Data cdd : in.value) {
                certs.add(cdd.x509);
            }
            return Result.ok(cout);
        } else {
            return Result.err(in);
        }
    }

    /* (non-Javadoc)
     * @see com.att.authz.certman.mapper.Mapper#toReq(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
     */
    @Override
    public Result<CertReq> toReq(AuthzTrans trans, BaseRequest req) {
        CertificateRequest in;
        try {
            in = (CertificateRequest)req;
        } catch (ClassCastException e) {
            return Result.err(Result.ERR_BadData,"Request is not a CertificateRequest");
        }

        CertReq out = new CertReq();
        CertmanValidator v = new CertmanValidator();
        out.mechid=in.getMechid();
        out.fqdns=in.getFqdns();
        v.isNull("CertRequest", req).nullOrBlank("MechID", out.mechid);
        v.nullBlankMin("FQDNs", out.fqdns,1);
        if (v.err()) {
            return Result.err(Result.ERR_BadData, v.errs());
        }
        out.emails = in.getEmail();
        out.sponsor=in.getSponsor();
        out.start = in.getStart();
        out.end = in.getEnd();
        out.fqdns = in.getFqdns();
        return Result.ok(out);
    }

    /* (non-Javadoc)
     * @see com.att.authz.certman.mapper.Mapper#toRenew(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
     */
    @Override
    public Result<CertRenew> toRenew(AuthzTrans trans, BaseRequest req) {
        return Result.err(Result.ERR_NotImplemented,"Not Implemented... yet");
    }

    /* (non-Javadoc)
     * @see com.att.authz.certman.mapper.Mapper#toDrop(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
     */
    @Override
    public Result<CertDrop> toDrop(AuthzTrans trans, BaseRequest req) {
        return Result.err(Result.ERR_NotImplemented,"Not Implemented... yet");
    }

    /* (non-Javadoc)
     * @see org.onap.aaf.auth.cm.mapper.Mapper#toArtifact(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
     */
    @Override
    public List<ArtiDAO.Data> toArtifact(AuthzTrans trans, Artifacts artifacts) {
        List<ArtiDAO.Data> ladd = new ArrayList<>();
        for (Artifact arti : artifacts.getArtifact()) {
            ArtiDAO.Data data = new ArtiDAO.Data();
            data.mechid = trim(arti.getMechid());
            data.machine = trim(arti.getMachine());
            if(arti.getType()!=null) {
	            Set<String> ss = data.type(true);
	            for(String t : arti.getType()) {
	            	ss.add(trim(t));
	            }
            }
            data.ca = trim(arti.getCa());
            data.dir = trim(arti.getDir());
            data.os_user = trim(arti.getOsUser());
            // Optional (on way in)
            data.ns = trim(arti.getNs());
            data.renewDays = arti.getRenewDays();
            data.notify = trim(arti.getNotification());
            
            // Ignored on way in for create/update
            data.sponsor = (arti.getSponsor());
            if(arti.getSans()!=null) {
                Set<String> ls = data.sans(true);
	            for(String t : arti.getSans()) {
	            	ls.add(trim(t));
	            }
            }
            data.expires = null;
            ladd.add(data);
        }
        return ladd;
    }

    /* (non-Javadoc)
     * @see org.onap.aaf.auth.cm.mapper.Mapper#fromArtifacts(org.onap.aaf.auth.layer.test.Result)
     */
    @Override
    public Result<Artifacts> fromArtifacts(Result<List<Data>> lArtiDAO) {
        if (lArtiDAO.isOK()) {
            Artifacts artis = new Artifacts();
            for (ArtiDAO.Data arti : lArtiDAO.value) {
                Artifact a = new Artifact();
                a.setMechid(trim(arti.mechid));
                a.setMachine(trim(arti.machine));
                a.setSponsor(trim(arti.sponsor));
                a.setNs(trim(arti.ns));
                a.setCa(trim(arti.ca));
                a.setDir(trim(arti.dir));
                for(String t : arti.type(false)) {
                	a.getType().add(trim(t));
                }
                a.setOsUser(trim(arti.os_user));
                a.setRenewDays(arti.renewDays);
                a.setNotification(trim(arti.notify));
                for(String t : arti.sans(false)) {
                	a.getSans().add(trim(t));
                }
                artis.getArtifact().add(a);
            }
            return Result.ok(artis);
        } else {
            return Result.err(lArtiDAO);
        }
    }
    
    

    private String trim(String s) {
    	if(s==null) {
    		return s;
    	} else {
    		return s.trim();
    	}
	}
}