Java Code Examples for org.apache.pulsar.broker.ServiceConfiguration#setProperties()

The following examples show how to use org.apache.pulsar.broker.ServiceConfiguration#setProperties() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthenticationProviderAthenzTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void setup() throws Exception {

    // Set provider domain name
    properties = new Properties();
    properties.setProperty("athenzDomainNames", "test_provider");
    config = new ServiceConfiguration();
    config.setProperties(properties);

    // Initialize authentication provider
    provider = new AuthenticationProviderAthenz();
    provider.initialize(config);

    // Specify Athenz configuration file for AuthZpeClient which is used in AuthenticationProviderAthenz
    System.setProperty(ZpeConsts.ZPE_PROP_ATHENZ_CONF, "./src/test/resources/athenz.conf.test");
}
 
Example 2
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test(expectedExceptions = AuthenticationException.class)
public void testAuthenticateWhenInvalidTokenIsPassed() throws AuthenticationException, IOException {
    SecretKey secretKey = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);

    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_SECRET_KEY,
            AuthTokenUtils.encodeKeyBase64(secretKey));

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);

    AuthenticationProviderToken provider = new AuthenticationProviderToken();
    provider.initialize(conf);
    provider.authenticate(new AuthenticationDataSource() {
        @Override
        public String getHttpHeader(String name) {
            return AuthenticationProviderToken.HTTP_HEADER_VALUE_PREFIX + "invalid_token";
        }

        @Override
        public boolean hasDataFromHttp() {
            return true;
        }
    });
}
 
Example 3
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = IOException.class)
public void testValidationKeyWhenBlankSecretKeyIsPassed() throws IOException {
    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_SECRET_KEY, "   ");

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);

    AuthenticationProviderToken provider = new AuthenticationProviderToken();
    provider.initialize(conf);
}
 
Example 4
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private static void testTokenAudienceWithDifferentConfig(Properties properties,
                                                         String audienceClaim, List<String> audiences) throws Exception {
    @Cleanup
    AuthenticationProviderToken provider = new AuthenticationProviderToken();
    SecretKey secretKey = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);

    File secretKeyFile = File.createTempFile("pulsar-test-secret-key-valid", ".key");
    secretKeyFile.deleteOnExit();
    Files.write(Paths.get(secretKeyFile.toString()), secretKey.getEncoded());

    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_SECRET_KEY, secretKeyFile.toString());
    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);

    String token = createTokenWithAudience(secretKey, audienceClaim, audiences);

    // Pulsar protocol auth
    String subject = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(subject, SUBJECT);
    provider.close();
}
 
Example 5
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testExpiringToken() throws Exception {
    SecretKey secretKey = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);

    @Cleanup
    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_SECRET_KEY,
            AuthTokenUtils.encodeKeyBase64(secretKey));

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);

    // Create a token that will expire in 3 seconds
    String expiringToken = AuthTokenUtils.createToken(secretKey, SUBJECT,
            Optional.of(new Date(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(3))));

    AuthenticationState authState = provider.newAuthState(AuthData.of(expiringToken.getBytes()), null, null);
    assertTrue(authState.isComplete());
    assertFalse(authState.isExpired());

    Thread.sleep(TimeUnit.SECONDS.toMillis(6));
    assertTrue(authState.isExpired());
    assertTrue(authState.isComplete());

    AuthData brokerData = authState.refreshAuthentication();
    assertNull(brokerData);
}
 
Example 6
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = IllegalArgumentException.class)
public void testValidationWhenPublicKeyAlgIsInvalid() throws IOException {
    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_ALG,
            "invalid");

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);

    new AuthenticationProviderToken().initialize(conf);
}
 
Example 7
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = IOException.class)
public void testInitializeWhenPublicKeyFilePathIsInvalid() throws IOException {
    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_KEY,
            "file://" + "invalid_public_key_file");

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);

    new AuthenticationProviderToken().initialize(conf);
}
 
Example 8
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = IllegalArgumentException.class)
public void testInitializeWhenSecretKeyFilePathIfNotExist() throws IOException {
    File secretKeyFile = File.createTempFile("secret_key_file_not_exist", ".key");
    assertTrue(secretKeyFile.delete());
    assertFalse(secretKeyFile.exists());

    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_SECRET_KEY, secretKeyFile.toString());

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);

    new AuthenticationProviderToken().initialize(conf);
}
 
Example 9
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = IOException.class)
public void testInitializeWhenSecretKeyIsValidPathOrBase64() throws IOException {
    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_SECRET_KEY,
            "secret_key_file_not_exist");

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);

    new AuthenticationProviderToken().initialize(conf);
}
 
Example 10
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = IOException.class)
public void testInitializeWhenSecretKeyFilePathIsInvalid() throws IOException {
    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_SECRET_KEY,
            "file://" + "invalid_secret_key_file");

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);

    new AuthenticationProviderToken().initialize(conf);
}
 
Example 11
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = IOException.class)
public void testValidationKeyWhenBlankPublicKeyIsPassed() throws IOException {
    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_KEY, "   ");

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);

    AuthenticationProviderToken provider = new AuthenticationProviderToken();
    provider.initialize(conf);
}
 
Example 12
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthSecretKeyPairWithECDSA() throws Exception {
    KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.ES256);

    String privateKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPrivate());
    String publicKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPublic());

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    // Use public key for validation
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_KEY, publicKeyStr);
    // Set that we are using EC keys
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_ALG, SignatureAlgorithm.ES256.getValue());

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);

    // Use private key to generate token
    PrivateKey privateKey = AuthTokenUtils.decodePrivateKey(Decoders.BASE64.decode(privateKeyStr), SignatureAlgorithm.ES256);
    String token = AuthTokenUtils.createToken(privateKey, SUBJECT, Optional.empty());

    // Pulsar protocol auth
    String subject = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(subject, SUBJECT);

    provider.close();
}
 
Example 13
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthSecretKeyPair() throws Exception {
    KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.RS256);

    String privateKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPrivate());
    String publicKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPublic());

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    // Use public key for validation
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_KEY, publicKeyStr);

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);

    // Use private key to generate token
    PrivateKey privateKey = AuthTokenUtils.decodePrivateKey(Decoders.BASE64.decode(privateKeyStr), SignatureAlgorithm.RS256);
    String token = AuthTokenUtils.createToken(privateKey, SUBJECT, Optional.empty());

    // Pulsar protocol auth
    String subject = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(subject, SUBJECT);

    provider.close();
}
 
Example 14
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthSecretKeyFromDataBase64() throws Exception {
    SecretKey secretKey = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_SECRET_KEY,
            "data:;base64," + AuthTokenUtils.encodeKeyBase64(secretKey));

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);

    String token = AuthTokenUtils.createToken(secretKey, SUBJECT, Optional.empty());

    // Pulsar protocol auth
    String subject = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(subject, SUBJECT);
    provider.close();
}
 
Example 15
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthSecretKeyFromValidFile() throws Exception {
    SecretKey secretKey = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);

    File secretKeyFile = File.createTempFile("pulsar-test-secret-key-valid", ".key");
    secretKeyFile.deleteOnExit();
    Files.write(Paths.get(secretKeyFile.toString()), secretKey.getEncoded());

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_SECRET_KEY, secretKeyFile.toString());

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);

    String token = AuthTokenUtils.createToken(secretKey, SUBJECT, Optional.empty());

    // Pulsar protocol auth
    String subject = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(subject, SUBJECT);
    provider.close();
}
 
Example 16
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test
public void testAuthSecretKeyFromFile() throws Exception {
    SecretKey secretKey = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);

    File secretKeyFile = File.createTempFile("pulsar-test-secret-key-", ".key");
    secretKeyFile.deleteOnExit();
    Files.write(Paths.get(secretKeyFile.toString()), secretKey.getEncoded());

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_SECRET_KEY, "file://" + secretKeyFile.toString());

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);

    String token = AuthTokenUtils.createToken(secretKey, SUBJECT, Optional.empty());

    // Pulsar protocol auth
    String subject = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(subject, SUBJECT);
    provider.close();
}
 
Example 17
Source File: PulsarAuthEnabledTest.java    From kop with Apache License 2.0 4 votes vote down vote up
@BeforeClass
@Override
protected void setup() throws Exception {
    SecretKey secretKey = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    properties.setProperty("tokenSecretKey", AuthTokenUtils.encodeKeyBase64(secretKey));
    ServiceConfiguration authConf = new ServiceConfiguration();
    authConf.setProperties(properties);
    provider.initialize(authConf);

    adminToken = AuthTokenUtils.createToken(secretKey, ADMIN_USER, Optional.empty());

    super.resetConfig();

    ((KafkaServiceConfiguration) conf).setKafkaTenant(TENANT);
    ((KafkaServiceConfiguration) conf).setKafkaNamespace(NAMESPACE);
    ((KafkaServiceConfiguration) conf).setKafkaMetadataTenant("internal");
    ((KafkaServiceConfiguration) conf).setKafkaMetadataNamespace("__kafka");
    ((KafkaServiceConfiguration) conf).setEnableGroupCoordinator(true);

    conf.setClusterName(CLUSTER_NAME);
    conf.setAuthorizationEnabled(true);
    conf.setAuthenticationEnabled(true);
    conf.setAuthorizationAllowWildcardsMatching(true);
    conf.setSuperUserRoles(Sets.newHashSet(ADMIN_USER));
    conf.setAuthenticationProviders(
        Sets.newHashSet("org.apache.pulsar.broker.authentication."
            + "AuthenticationProviderToken"));
    conf.setBrokerClientAuthenticationPlugin(AuthenticationToken.class.getName());
    conf.setBrokerClientAuthenticationParameters("token:" + adminToken);
    conf.setProperties(properties);

    super.internalSetup();

    admin = spy(PulsarAdmin.builder().serviceHttpUrl(brokerUrl.toString())
        .authentication(AuthenticationToken.class.getName(), "token:" + adminToken).build());

    admin.tenants().createTenant(TENANT,
        new TenantInfo(Sets.newHashSet(ADMIN_USER), Sets.newHashSet(CLUSTER_NAME)));
    admin.namespaces().createNamespace(TENANT + "/" + NAMESPACE);
    admin.namespaces()
        .setNamespaceReplicationClusters(TENANT + "/" + NAMESPACE, Sets.newHashSet(CLUSTER_NAME));
    admin.topics().createPartitionedTopic(PULSAR_TOPIC_NAME, 1);
    admin.namespaces().grantPermissionOnNamespace(TENANT + "/" + NAMESPACE, ADMIN_USER,
        Sets.newHashSet(AuthAction.consume, AuthAction.produce));
}
 
Example 18
Source File: AuthenticationProviderTokenTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Test
public void testAuthSecretKeyPairWithCustomClaim() throws Exception {
    String authRoleClaim = "customClaim";
    String authRole = "my-test-role";

    KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.RS256);

    String privateKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPrivate());
    String publicKeyStr = AuthTokenUtils.encodeKeyBase64(keyPair.getPublic());

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    // Use public key for validation
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_PUBLIC_KEY, publicKeyStr);
    // Set custom claim field
    properties.setProperty(AuthenticationProviderToken.CONF_TOKEN_AUTH_CLAIM, authRoleClaim);

    ServiceConfiguration conf = new ServiceConfiguration();
    conf.setProperties(properties);
    provider.initialize(conf);


    // Use private key to generate token
    PrivateKey privateKey = AuthTokenUtils.decodePrivateKey(Decoders.BASE64.decode(privateKeyStr), SignatureAlgorithm.RS256);
    String token = Jwts.builder()
            .setClaims(new HashMap<String, Object>() {{
                put(authRoleClaim, authRole);
            }})
            .signWith(privateKey)
            .compact();


    // Pulsar protocol auth
    String role = provider.authenticate(new AuthenticationDataSource() {
        @Override
        public boolean hasDataFromCommand() {
            return true;
        }

        @Override
        public String getCommandData() {
            return token;
        }
    });
    assertEquals(role, authRole);

    provider.close();
}
 
Example 19
Source File: SaslPlainTest.java    From kop with Apache License 2.0 4 votes vote down vote up
@BeforeClass
@Override
protected void setup() throws Exception {
    SecretKey secretKey = AuthTokenUtils.createSecretKey(SignatureAlgorithm.HS256);

    AuthenticationProviderToken provider = new AuthenticationProviderToken();

    Properties properties = new Properties();
    properties.setProperty("tokenSecretKey", AuthTokenUtils.encodeKeyBase64(secretKey));
    ServiceConfiguration authConf = new ServiceConfiguration();
    authConf.setProperties(properties);
    provider.initialize(authConf);

    userToken = AuthTokenUtils.createToken(secretKey, SIMPLE_USER, Optional.empty());
    adminToken = AuthTokenUtils.createToken(secretKey, ADMIN_USER, Optional.empty());
    anotherToken = AuthTokenUtils.createToken(secretKey, ANOTHER_USER, Optional.empty());

    super.resetConfig();
    ((KafkaServiceConfiguration) conf).setEnableGroupCoordinator(true);
    ((KafkaServiceConfiguration) conf).setSaslAllowedMechanisms(Sets.newHashSet("PLAIN"));
    ((KafkaServiceConfiguration) conf).setKafkaMetadataTenant("internal");
    ((KafkaServiceConfiguration) conf).setKafkaMetadataNamespace("__kafka");

    conf.setClusterName(CLUSTER_NAME);
    conf.setAuthorizationEnabled(true);
    conf.setAuthenticationEnabled(true);
    conf.setAuthorizationAllowWildcardsMatching(true);
    conf.setSuperUserRoles(Sets.newHashSet(ADMIN_USER));
    conf.setAuthenticationProviders(
        Sets.newHashSet("org.apache.pulsar.broker.authentication."
            + "AuthenticationProviderToken"));
    conf.setBrokerClientAuthenticationPlugin(AuthenticationToken.class.getName());
    conf.setBrokerClientAuthenticationParameters("token:" + adminToken);
    conf.setProperties(properties);

    super.internalSetup();

    admin = spy(PulsarAdmin.builder().serviceHttpUrl(brokerUrl.toString())
        .authentication(AuthenticationToken.class.getName(), "token:" + adminToken).build());

    admin.tenants().createTenant(TENANT,
        new TenantInfo(Sets.newHashSet(ADMIN_USER), Sets.newHashSet(CLUSTER_NAME)));
    admin.namespaces().createNamespace(TENANT + "/" + NAMESPACE);
    admin.topics().createPartitionedTopic(PULSAR_TOPIC_NAME, 1);
    admin
        .namespaces().grantPermissionOnNamespace(TENANT + "/" + NAMESPACE, SIMPLE_USER,
        Sets.newHashSet(AuthAction.consume, AuthAction.produce));
}