Skip to content

Commit 33fed36

Browse files
committed
externalized rule head property, fixed ConfigLoader
1 parent 8ea863f commit 33fed36

6 files changed

Lines changed: 22 additions & 15 deletions

File tree

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,19 @@ This builds the ``.nar``-plugin in the ``/target`` folder.
5050

5151
In order to use the plugin in Apache NiFi the following steps must be performed:
5252
1. Go to the ``/opt/nifi/nifi-current/conf/authorizers.xml`` in your Apache NiFi Instance / Container
53-
2. Place the the following ``opa-authorizer``-snippet in the file (e.g. beneath the ``managed-authorizer``):
53+
2. Place the the following ``opa-authorizer``-snippet in the file (for example beneath the ``managed-authorizer``):
5454
````xml
55-
<!-- Overriding the normal managed-authorizer entry -->
55+
<!-- example snippet -->
5656
<authorizer>
5757
<identifier>opa-authorizer</identifier>
5858
<class>org.nifiopa.nifiopa.OpaAuthorizer</class>
5959
<property name="CACHE_TIME_SECS">30</property>
6060
<property name="CACHE_MAX_ENTRY_COUNT">100</property>
61-
<property name="OPA_URI">http://opa:8181/</property>
61+
<property name="OPA_URI">http://opa:8181/</property> <!--required-->
62+
<property name="OPA_RULE_HEAD">nifi/allow</property> <!--required-->
6263
</authorizer>
6364
````
65+
Alternatively, the properties fields can also be set as environment variables.
6466
3. Set the env-variable ``NIFI_SECURITY_USER_AUTHORIZER`` of Apache NiFi **or** the ``nifi.security.user.authorizer`` in Apache NiFis nifi.properties-file to ``opa-authorizer``.
6567
4. Place the ``nar``-plugin you build aboth in the ``/opt/nifi/nifi-current/extensions/`` folder of your Apache NiFi Instance / Container
6668
5. Restart Apache NiFi
@@ -72,6 +74,7 @@ The following properties can be configurated in the ``authorizers.xml`` or using
7274
| Property Key | Example | Default | Description |
7375
| --- | --- | --- | --- |
7476
| `OPA_URI` | `http://opa:8181/` | | Endpoint of the OPA policy to query. **required** |
77+
| `OPA_RULE_HEAD` | `nifi/allow` | | Rule head against which the query is made (*package*/*rule*). **required** |
7578
| `CACHE_TIME_SECS` | `30` | `30` | Maximum time in seconds an entry in the decision cache exists. |
7679
| `CACHE_MAX_ENTRY_COUNT` | `100` | `0` | Maximum entries of the decision cache. |
7780

authorizer/src/main/java/org/nifiopa/nifiopa/ConfigLoader.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@
1111
public class ConfigLoader {
1212

1313
private static final Logger logger = LoggerFactory.getLogger(ConfigLoader.class);
14-
private static AuthorizerConfigurationContext configurationContext;
1514

16-
public ConfigLoader(AuthorizerConfigurationContext configurationContext) {
17-
ConfigLoader.configurationContext = configurationContext;
18-
}
19-
20-
public static String getProperty(String propertyName, String defaultValue) throws InvalidParameterException {
15+
public static String getProperty(AuthorizerConfigurationContext configurationContext, String propertyName, String defaultValue) throws InvalidParameterException {
2116
// 1. Try load from environment
2217
try {
2318
String propertyEnv = System.getenv(propertyName);
@@ -43,8 +38,8 @@ public static String getProperty(String propertyName, String defaultValue) throw
4338
return defaultValue;
4439
}
4540

46-
public static String getProperty(String propertyName) throws InvalidParameterException {
47-
String property = ConfigLoader.getProperty(propertyName, null);
41+
public static String getProperty(AuthorizerConfigurationContext configurationContext, String propertyName) throws InvalidParameterException {
42+
String property = ConfigLoader.getProperty(configurationContext, propertyName, null);
4843
if (property != null)
4944
return property;
5045
throw new InvalidParameterException(

authorizer/src/main/java/org/nifiopa/nifiopa/OpaAuthorizer.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public class OpaAuthorizer implements Authorizer {
2525

2626
private final String OPA_URI_PROPNAME = "OPA_URI";
2727

28+
private final String OPA_RULE_HEAD_PROPNAME = "OPA_RULE_HEAD";
29+
private String OPA_RULE_HEAD;
30+
2831
@Override
2932
public AuthorizationResult authorize(AuthorizationRequest request) throws AuthorizationAccessException {
3033

@@ -79,7 +82,7 @@ public AuthorizationResult authorize(AuthorizationRequest request) throws Author
7982

8083
OPAResponse opaResponse = null;
8184
try {
82-
opaResponse = opaClient.evaluate("nifi/allow", requestForm, OPAResponse.class); // TODO: rule not hardcoded
85+
opaResponse = opaClient.evaluate(OPA_RULE_HEAD, requestForm, OPAResponse.class);
8386
} catch (OPAException e) {
8487
logger.error(MessageFormat.format("An error occured while trying to query against OPA: {0}", e.toString()));
8588
return AuthorizationResult.denied("An error occured while trying to query against OPA");
@@ -121,10 +124,14 @@ public void initialize(AuthorizerInitializationContext initializationContext) th
121124

122125
@Override
123126
public void onConfigured(AuthorizerConfigurationContext configurationContext) throws AuthorizerCreationException {
124-
String uriProp = ConfigLoader.getProperty(OPA_URI_PROPNAME);
127+
String uriProp = ConfigLoader.getProperty(configurationContext, OPA_URI_PROPNAME);
125128
if (uriProp == null)
126129
throw new AuthorizerCreationException("Missing required property OPA_URI");
127130

131+
OPA_RULE_HEAD = ConfigLoader.getProperty(configurationContext, OPA_RULE_HEAD_PROPNAME);
132+
if (OPA_RULE_HEAD == null)
133+
throw new AuthorizerCreationException("Missing required property OPA_RULE_HEAD");
134+
128135
opaClient = new OPAClient(uriProp);
129136
cache = new RequestCache();
130137
cache.initialize(configurationContext);

authorizer/src/main/java/org/nifiopa/nifiopa/RequestCache.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void initialize(AuthorizerConfigurationContext configurationContext) thro
5757
void initializePropertys(AuthorizerConfigurationContext configurationContext) {
5858
try {
5959
/* Initialize property of maximum cache time after write */
60-
String cacheTimeProp = ConfigLoader.getProperty(CACHE_TIME_SECS_PROPNAME, CACHE_TIME_SECS_DEFAULT);
60+
String cacheTimeProp = ConfigLoader.getProperty(configurationContext, CACHE_TIME_SECS_PROPNAME, CACHE_TIME_SECS_DEFAULT);
6161
if (cacheTimeProp != null)
6262
CACHE_TIME_SECS = Integer.parseInt(cacheTimeProp);
6363
} catch (NumberFormatException nfe) {
@@ -70,7 +70,7 @@ void initializePropertys(AuthorizerConfigurationContext configurationContext) {
7070

7171
/* Initialize property of maximum entries in the cache at one time */
7272
try {
73-
String cacheMaxEntryProp = ConfigLoader.getProperty(CACHE_MAX_ENTRY_COUNT_PROPNAME, CACHE_MAX_ENTRY_COUNT_DEFAULT);
73+
String cacheMaxEntryProp = ConfigLoader.getProperty(configurationContext, CACHE_MAX_ENTRY_COUNT_PROPNAME, CACHE_MAX_ENTRY_COUNT_DEFAULT);
7474
if (cacheMaxEntryProp != null)
7575
CACHE_MAX_ENTRY_COUNT = Integer.parseInt(cacheMaxEntryProp);
7676
} catch (NumberFormatException nfe) {

test-env/authorizers-opa.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@
356356
<property name="CACHE_TIME_SECS">30</property>
357357
<property name="CACHE_MAX_ENTRY_COUNT">100</property>
358358
<property name="OPA_URI">http://opa:8181/</property>
359+
<property name="OPA_RULE_HEAD">nifi/allow</property>
359360
</authorizer>
360361

361362
<!--

test-env/compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ services:
5656
LDAP_IDENTITY_STRATEGY: 'USE_USERNAME'
5757
LDAP_URL: ldap://openldap:1389
5858
OPA_URI: "http://opa:8181/"
59+
OPA_RULE_HEAD: "nifi/allow"
5960
NIFI_SECURITY_USER_AUTHORIZER: "opa-authorizer"
6061
networks:
6162
- test-network

0 commit comments

Comments
 (0)