forked from apache/nifi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NIFI-14026 Added proxied-entity module for web-security and toolkit
- Added ProxiedEntityEncoder interface and implementation - Removed nifi-web-security dependency from nifi-toolkit-client - Removed Google Guava dependency from nifi-toolkit-cli
- Loading branch information
1 parent
df793ce
commit 1bc264b
Showing
14 changed files
with
242 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<!-- | ||
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 | ||
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. | ||
--> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.apache.nifi</groupId> | ||
<artifactId>nifi-commons</artifactId> | ||
<version>2.1.0-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>nifi-security-proxied-entity</artifactId> | ||
<description>Shared security components for handling Proxied Entities in HTTP requests and response</description> | ||
</project> | ||
|
29 changes: 29 additions & 0 deletions
29
...ed-entity/src/main/java/org/apache/nifi/security/proxied/entity/ProxiedEntityEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
package org.apache.nifi.security.proxied.entity; | ||
|
||
/** | ||
* Proxied Entity Encoder sanitizes and formats identities for transmission as HTTP request headers | ||
*/ | ||
public interface ProxiedEntityEncoder { | ||
/** | ||
* Get encoded representation of identity suitable for transmission | ||
* @param identity Identity to be encoded | ||
* @return Encoded Entity | ||
*/ | ||
String getEncodedEntity(String identity); | ||
} |
89 changes: 89 additions & 0 deletions
89
...y/src/main/java/org/apache/nifi/security/proxied/entity/StandardProxiedEntityEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
package org.apache.nifi.security.proxied.entity; | ||
|
||
import java.nio.charset.CharsetEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
/** | ||
* Standard implementation singleton instance of Proxied Entity Encoder | ||
*/ | ||
public class StandardProxiedEntityEncoder implements ProxiedEntityEncoder { | ||
private static final String DELIMITED_FORMAT = "<%s>"; | ||
|
||
private static final String GT = ">"; | ||
|
||
private static final String ESCAPED_GT = "\\\\>"; | ||
|
||
private static final String LT = "<"; | ||
|
||
private static final String ESCAPED_LT = "\\\\<"; | ||
|
||
private static final CharsetEncoder headerValueCharsetEncoder = StandardCharsets.US_ASCII.newEncoder(); | ||
|
||
private static final Base64.Encoder headerValueEncoder = Base64.getEncoder(); | ||
|
||
private static final StandardProxiedEntityEncoder encoder = new StandardProxiedEntityEncoder(); | ||
|
||
private StandardProxiedEntityEncoder() { | ||
|
||
} | ||
|
||
/** | ||
* Get singleton instance of standard encoder | ||
* | ||
* @return Proxied Entity Encoder | ||
*/ | ||
public static ProxiedEntityEncoder getInstance() { | ||
return encoder; | ||
} | ||
|
||
/** | ||
* Get encoded entity sanitized and formatted for transmission as an HTTP header | ||
* | ||
* @param identity Identity to be encoded | ||
* @return Encoded Entity | ||
*/ | ||
@Override | ||
public String getEncodedEntity(final String identity) { | ||
final String sanitizedIdentity = getSanitizedIdentity(identity); | ||
return DELIMITED_FORMAT.formatted(sanitizedIdentity); | ||
} | ||
|
||
private String getSanitizedIdentity(final String identity) { | ||
final String sanitized; | ||
|
||
if (identity == null || identity.isEmpty()) { | ||
sanitized = identity; | ||
} else { | ||
final String escaped = identity.replaceAll(LT, ESCAPED_LT).replaceAll(GT, ESCAPED_GT); | ||
|
||
if (headerValueCharsetEncoder.canEncode(escaped)) { | ||
// Strings limited to US-ASCII characters can be transmitted as HTTP header values without encoding | ||
sanitized = escaped; | ||
} else { | ||
// Non-ASCII characters require Base64 encoding and additional wrapping for transmission as headers | ||
final byte[] escapedBinary = escaped.getBytes(StandardCharsets.UTF_8); | ||
final String escapedEncoded = headerValueEncoder.encodeToString(escapedBinary); | ||
sanitized = DELIMITED_FORMAT.formatted(escapedEncoded); | ||
} | ||
} | ||
|
||
return sanitized; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...c/test/java/org/apache/nifi/security/proxied/entity/StandardProxiedEntityEncoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* 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 | ||
* | ||
* 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. | ||
*/ | ||
package org.apache.nifi.security.proxied.entity; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class StandardProxiedEntityEncoderTest { | ||
|
||
@ParameterizedTest | ||
@CsvSource( | ||
delimiter = ':', | ||
value = { | ||
":<null>", | ||
"'':<>", | ||
"username:<username>", | ||
"CN=Common Name, OU=Organizational Unit:<CN=Common Name, OU=Organizational Unit>", | ||
"CN=nifi.apache.org, O=Organization:<CN=nifi.apache.org, O=Organization>", | ||
"<username>:<\\<username\\>>", | ||
"<<username>>:<\\<\\<username\\>\\>>", | ||
"CN=\uD83D\uDE00:<<Q0498J+YgA==>>" | ||
} | ||
) | ||
void testGetEncodedEntity(final String identity, final String expected) { | ||
final String encodedEntity = StandardProxiedEntityEncoder.getInstance().getEncodedEntity(identity); | ||
|
||
assertEquals(expected, encodedEntity); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.