|
15 | 15 | */ |
16 | 16 | package it.infn.mw.voms.api; |
17 | 17 |
|
| 18 | +import static java.lang.String.format; |
| 19 | + |
18 | 20 | import java.io.IOException; |
| 21 | +import java.text.SimpleDateFormat; |
19 | 22 |
|
20 | | -import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
21 | 25 | import org.springframework.security.access.prepost.PreAuthorize; |
22 | 26 | import org.springframework.security.core.Authentication; |
23 | 27 | import org.springframework.transaction.annotation.Transactional; |
|
29 | 33 |
|
30 | 34 | import it.infn.mw.iam.authn.x509.IamX509AuthenticationCredential; |
31 | 35 | import it.infn.mw.iam.persistence.model.IamAccount; |
32 | | -import it.infn.mw.iam.persistence.repository.IamAccountRepository; |
33 | 36 | import it.infn.mw.iam.service.aup.AUPSignatureCheckService; |
34 | 37 | import it.infn.mw.voms.aa.AttributeAuthority; |
35 | 38 | import it.infn.mw.voms.aa.RequestContextFactory; |
36 | 39 | import it.infn.mw.voms.aa.VOMSErrorMessage; |
| 40 | +import it.infn.mw.voms.aa.VOMSRequest; |
37 | 41 | import it.infn.mw.voms.aa.VOMSRequestContext; |
| 42 | +import it.infn.mw.voms.aa.VOMSResponse; |
| 43 | +import it.infn.mw.voms.aa.VOMSResponse.Outcome; |
38 | 44 | import it.infn.mw.voms.aa.ac.ACGenerator; |
39 | 45 | import it.infn.mw.voms.aa.ac.VOMSResponseBuilder; |
40 | 46 | import it.infn.mw.voms.properties.VomsProperties; |
|
44 | 50 | @Transactional |
45 | 51 | public class VOMSController extends VOMSControllerSupport { |
46 | 52 |
|
| 53 | + private final Logger log = LoggerFactory.getLogger(VOMSController.class); |
| 54 | + |
47 | 55 | public static final String LEGACY_VOMS_APIS_UA = "voms APIs 2.0"; |
48 | 56 |
|
| 57 | + private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); |
| 58 | + |
49 | 59 | private final VomsProperties vomsProperties; |
50 | 60 | private final AttributeAuthority aa; |
51 | 61 | private final ACGenerator acGenerator; |
52 | 62 | private final VOMSResponseBuilder responseBuilder; |
53 | 63 | private final AUPSignatureCheckService signatureCheckService; |
54 | 64 |
|
55 | | - @Autowired |
56 | 65 | public VOMSController(AttributeAuthority aa, VomsProperties props, ACGenerator acGenerator, |
57 | | - VOMSResponseBuilder responseBuilder, IamAccountRepository accountRepo, |
58 | | - AUPSignatureCheckService signatureCheckService) { |
| 66 | + VOMSResponseBuilder responseBuilder, AUPSignatureCheckService signatureCheckService) { |
59 | 67 | this.aa = aa; |
60 | 68 | this.vomsProperties = props; |
61 | 69 | this.acGenerator = acGenerator; |
@@ -102,24 +110,79 @@ public String generateAC(@RequestHeader(name = "User-Agent", required = false) S |
102 | 110 | (IamX509AuthenticationCredential) authentication.getCredentials(); |
103 | 111 |
|
104 | 112 | VOMSRequestContext context = initVomsRequestContext(cred, request, userAgent); |
| 113 | + logRequest(context); |
105 | 114 |
|
106 | 115 | if (!aa.getAttributes(context)) { |
107 | 116 |
|
108 | 117 | VOMSErrorMessage em = context.getResponse().getErrorMessages().get(0); |
109 | 118 |
|
| 119 | + String responseString; |
110 | 120 | if (LEGACY_VOMS_APIS_UA.equals(userAgent)) { |
111 | | - return responseBuilder.createLegacyErrorResponse(em); |
| 121 | + responseString = responseBuilder.createLegacyErrorResponse(em); |
112 | 122 | } else { |
113 | | - return responseBuilder.createErrorResponse(em); |
| 123 | + responseString = responseBuilder.createErrorResponse(em); |
114 | 124 | } |
| 125 | + logOutcome(context); |
| 126 | + return responseString; |
115 | 127 | } else { |
116 | 128 | IamAccount user = context.getIamAccount(); |
117 | 129 | if (signatureCheckService.needsAupSignature(user)) { |
118 | 130 | VOMSErrorMessage em = VOMSErrorMessage.faildToSignAup(user.getUsername()); |
119 | 131 | return responseBuilder.createErrorResponse(em); |
120 | 132 | } |
121 | 133 | byte[] acBytes = acGenerator.generateVOMSAC(context); |
122 | | - return responseBuilder.createResponse(acBytes, context.getResponse().getWarnings()); |
| 134 | + String responseString = |
| 135 | + responseBuilder.createResponse(acBytes, context.getResponse().getWarnings()); |
| 136 | + logOutcome(context); |
| 137 | + return responseString; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private void logRequest(VOMSRequestContext c) { |
| 142 | + if (log.isDebugEnabled()) { |
| 143 | + VOMSRequest r = c.getRequest(); |
| 144 | + log.debug( |
| 145 | + "VOMSRequest: [holderIssuer: {}, holderSubject: {}, requesterIssuer: {}, requesterSubject: {}, attributes: {}, FQANs: {}, validity: {}, targets: {}]", |
| 146 | + sanitize(r.getHolderIssuer()), sanitize(r.getHolderSubject()), |
| 147 | + sanitize(r.getRequesterIssuer()), sanitize(r.getRequesterSubject()), |
| 148 | + r.getRequestAttributes(), r.getRequestedFQANs(), r.getRequestedValidity(), r.getTargets()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + private String sanitize(String str) { |
| 153 | + return str.replaceAll("[\n\r]", "_"); |
| 154 | + } |
| 155 | + |
| 156 | + private String userStr(VOMSRequestContext c) { |
| 157 | + String username = c.getIamAccount().getUsername(); |
| 158 | + String uuid = c.getIamAccount().getUuid(); |
| 159 | + String reqSubject = c.getRequest().getRequesterSubject(); |
| 160 | + String reqIssuer = c.getRequest().getRequesterIssuer(); |
| 161 | + return sanitize(format("[username: %s, uuid: %s, subjectDN: %s, issuerDN: %s]", username, uuid, |
| 162 | + reqSubject, reqIssuer)); |
| 163 | + } |
| 164 | + |
| 165 | + private String errorResponse(VOMSRequestContext c) { |
| 166 | + return sanitize(format("[outcome: %s, errorMessages: %s]", c.getResponse().getOutcome().name(), |
| 167 | + c.getResponse().getErrorMessages())); |
| 168 | + } |
| 169 | + |
| 170 | + private String successResponse(VOMSRequestContext c) { |
| 171 | + VOMSResponse r = c.getResponse(); |
| 172 | + return sanitize(format( |
| 173 | + "[outcome: %s, VO: %s, uri: %s, targets: %s, issuedFQANs: %s, notAfter: %s, notBefore: %s]", |
| 174 | + r.getOutcome().name(), c.getVOName(), c.getHost() + ":" + c.getPort(), |
| 175 | + r.getTargets().toString(), r.getIssuedFQANs().toString(), |
| 176 | + dateFormat.format(r.getNotAfter()), dateFormat.format(r.getNotBefore()))); |
| 177 | + } |
| 178 | + |
| 179 | + private void logOutcome(VOMSRequestContext c) { |
| 180 | + if (log.isInfoEnabled()) { |
| 181 | + if (Outcome.SUCCESS.equals(c.getResponse().getOutcome())) { |
| 182 | + log.info("User {} got successful VOMS response {} ", userStr(c), successResponse(c)); |
| 183 | + } else { |
| 184 | + log.info("User {} got failure VOMS response {}", userStr(c), errorResponse(c)); |
| 185 | + } |
123 | 186 | } |
124 | 187 | } |
125 | 188 | } |
0 commit comments