-
Notifications
You must be signed in to change notification settings - Fork 474
/
AuthorizationFilter.java
125 lines (109 loc) · 4.43 KB
/
AuthorizationFilter.java
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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.
*/
package org.apache.hadoop.ozone.s3;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import com.google.common.annotations.VisibleForTesting;
import org.apache.hadoop.ozone.s3.exception.OS3Exception;
import org.apache.hadoop.ozone.s3.exception.S3ErrorTable;
import org.apache.hadoop.ozone.s3.signature.SignatureInfo;
import org.apache.hadoop.ozone.s3.signature.SignatureInfo.Version;
import org.apache.hadoop.ozone.s3.signature.SignatureProcessor;
import org.apache.hadoop.ozone.s3.signature.StringToSignProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.ACCESS_DENIED;
import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.INTERNAL_ERROR;
import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.S3_AUTHINFO_CREATION_ERROR;
/**
* Filter used to construct string to sign from unfiltered request.
* It should be executed before all other filters as the original
* could be enriched later.
*/
@Provider
@PreMatching
@Priority(AuthorizationFilter.PRIORITY)
public class AuthorizationFilter implements ContainerRequestFilter {
public static final int PRIORITY = 50;
private static final Logger LOG = LoggerFactory.getLogger(
AuthorizationFilter.class);
@Inject
private SignatureProcessor signatureProcessor;
@Inject
private SignatureInfo signatureInfo;
@Override
public void filter(ContainerRequestContext context) throws
IOException {
// Skip authentication if the uri is hitting S3Secret generation or
// revocation endpoint.
if (context.getUriInfo().getRequestUri().getPath().startsWith("/secret")) {
return;
}
try {
signatureInfo.initialize(signatureProcessor.parseSignature());
if (signatureInfo.getVersion() == Version.V4) {
signatureInfo.setStrToSign(
StringToSignProducer.createSignatureBase(signatureInfo, context));
} else {
LOG.debug("Unsupported AWS signature version: {}",
signatureInfo.getVersion());
throw S3_AUTHINFO_CREATION_ERROR;
}
String awsAccessId = signatureInfo.getAwsAccessId();
// ONLY validate aws access id when needed.
if (awsAccessId == null || awsAccessId.equals("")) {
LOG.debug("Malformed s3 header. awsAccessID: {}", awsAccessId);
throw ACCESS_DENIED;
}
} catch (OS3Exception ex) {
LOG.debug("Error during Client Creation: ", ex);
throw wrapOS3Exception(ex);
} catch (Exception e) {
// For any other critical errors during object creation throw Internal
// error.
LOG.debug("Error during Client Creation: ", e);
throw wrapOS3Exception(
S3ErrorTable.newError(INTERNAL_ERROR, null, e));
}
}
@VisibleForTesting
public void setSignatureParser(SignatureProcessor awsSignatureProcessor) {
this.signatureProcessor = awsSignatureProcessor;
}
@VisibleForTesting
public void setSignatureInfo(SignatureInfo signatureInfo) {
this.signatureInfo = signatureInfo;
}
@VisibleForTesting
public SignatureInfo getSignatureInfo() {
return signatureInfo;
}
private WebApplicationException wrapOS3Exception(OS3Exception os3Exception) {
return new WebApplicationException(os3Exception.getErrorMessage(),
os3Exception,
Response.status(os3Exception.getHttpCode())
.entity(os3Exception.toXml()).build());
}
}