forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing authentication for Pulsar Functions (apache#3735)
* Implementing authentication for Pulsar Functions * delete unnecessary changes * cleaning up * improving implementation * fixing tests * cleaning up * add no op implementation * cleaning up unnecessary changes * refactoring based on comments * adding comments * change data from string type to bytes * add proto file * addressing comments * up merging * refactoring get token code * cleaning up * fix bugs and add tests * add tests * remove service account creation * cleanup unused imports * add field for auth provider * adding comments
- Loading branch information
Showing
33 changed files
with
921 additions
and
80 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
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
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
55 changes: 55 additions & 0 deletions
55
...me/src/main/java/org/apache/pulsar/functions/auth/ClearTextFunctionTokenAuthProvider.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,55 @@ | ||
/** | ||
* 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.pulsar.functions.auth; | ||
|
||
import org.apache.pulsar.broker.authentication.AuthenticationDataSource; | ||
import org.apache.pulsar.client.impl.auth.AuthenticationToken; | ||
import org.apache.pulsar.functions.instance.AuthenticationConfig; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.apache.pulsar.broker.authentication.AuthenticationProviderToken.getToken; | ||
|
||
public class ClearTextFunctionTokenAuthProvider implements FunctionAuthProvider { | ||
@Override | ||
public void configureAuthenticationConfig(AuthenticationConfig authConfig, FunctionAuthData functionAuthData) { | ||
authConfig.setClientAuthenticationPlugin(AuthenticationToken.class.getName()); | ||
authConfig.setClientAuthenticationParameters("token:" + new String(functionAuthData.getData())); | ||
} | ||
|
||
@Override | ||
public Optional<FunctionAuthData> cacheAuthData(String tenant, String namespace, String name, AuthenticationDataSource authenticationDataSource) throws Exception { | ||
String token = null; | ||
try { | ||
token = getToken(authenticationDataSource); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
if (token != null) { | ||
return Optional.of(FunctionAuthData.builder().data(token.getBytes()).build()); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void cleanUpAuthData(String tenant, String namespace, String name, FunctionAuthData functionAuthData) throws Exception { | ||
//no-op | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...ar-functions/runtime/src/main/java/org/apache/pulsar/functions/auth/FunctionAuthData.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,41 @@ | ||
/** | ||
* 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.pulsar.functions.auth; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
/** | ||
* A wrapper for authentication data for functions | ||
*/ | ||
public class FunctionAuthData { | ||
/** | ||
* function authentication related data that the function authentication provider | ||
* needs to cache/distribute to all workers support function authentication. | ||
* Depending on the function authentication provider implementation, this can be the actual auth credentials | ||
* or a pointer to the auth credentials that this function should use | ||
*/ | ||
private byte[] data; | ||
/** | ||
* classname of the function auth provicer this data is relevant to | ||
*/ | ||
private String provider; | ||
} |
59 changes: 59 additions & 0 deletions
59
...unctions/runtime/src/main/java/org/apache/pulsar/functions/auth/FunctionAuthProvider.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,59 @@ | ||
/** | ||
* 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.pulsar.functions.auth; | ||
|
||
import org.apache.pulsar.broker.authentication.AuthenticationDataSource; | ||
import org.apache.pulsar.functions.instance.AuthenticationConfig; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* This is a generic interface that functions can use to cache and distribute appropriate authentication | ||
* data that is needed to configure the runtime of functions to support appropriate authentication of function instances | ||
*/ | ||
public interface FunctionAuthProvider { | ||
|
||
/** | ||
* Set authentication configs for function instance based on the data in FunctionAuthenticationSpec | ||
* @param authConfig authentication configs passed to the function instance | ||
* @param functionAuthData function authentication data that is provider specific | ||
*/ | ||
void configureAuthenticationConfig(AuthenticationConfig authConfig, FunctionAuthData functionAuthData); | ||
|
||
/** | ||
* Cache auth data in as part of function metadata for function that runtime may need to configure authentication | ||
* @param tenant tenant that the function is running under | ||
* @param namespace namespace that is the function is running under | ||
* @param name name of the function | ||
* @param authenticationDataSource auth data | ||
* @return | ||
* @throws Exception | ||
*/ | ||
Optional<FunctionAuthData> cacheAuthData(String tenant, String namespace, String name, AuthenticationDataSource authenticationDataSource) throws Exception; | ||
|
||
/** | ||
* Clean up operation for auth when function is terminated | ||
* @param tenant tenant that the function is running under | ||
* @param namespace namespace that is the function is running under | ||
* @param name name of the function | ||
* @param functionAuthData function auth data | ||
* @throws Exception | ||
*/ | ||
void cleanUpAuthData(String tenant, String namespace, String name, FunctionAuthData functionAuthData) throws Exception; | ||
} |
28 changes: 28 additions & 0 deletions
28
...r-functions/runtime/src/main/java/org/apache/pulsar/functions/auth/FunctionAuthUtils.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,28 @@ | ||
/** | ||
* 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.pulsar.functions.auth; | ||
|
||
import org.apache.pulsar.functions.proto.Function; | ||
|
||
public final class FunctionAuthUtils { | ||
|
||
public static final FunctionAuthData getFunctionAuthData(Function.FunctionAuthenticationSpec functionAuthenticationSpec) { | ||
return FunctionAuthData.builder().data(functionAuthenticationSpec.getData().toByteArray()).build(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...untime/src/main/java/org/apache/pulsar/functions/auth/KubernetesFunctionAuthProvider.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,35 @@ | ||
/** | ||
* 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.pulsar.functions.auth; | ||
|
||
import io.kubernetes.client.models.V1ServiceAccount; | ||
import io.kubernetes.client.models.V1StatefulSet; | ||
|
||
/** | ||
* Kubernetes runtime specific functions authentication provider | ||
*/ | ||
public interface KubernetesFunctionAuthProvider extends FunctionAuthProvider { | ||
|
||
/** | ||
* Configure function statefulset spec based on function auth data | ||
* @param statefulSet statefulset spec for function | ||
* @param functionAuthData function auth data | ||
*/ | ||
void configureAuthDataStatefulSet(V1StatefulSet statefulSet, FunctionAuthData functionAuthData); | ||
} |
Oops, something went wrong.