@Test public void testCacheAuthData() throws ApiException { CoreV1Api coreV1Api = mock(CoreV1Api.class); doReturn(new V1Secret()).when(coreV1Api).createNamespacedSecret(anyString(), any(), anyString(), anyString(), anyString()); KubernetesSecretsTokenAuthProvider kubernetesSecretsTokenAuthProvider = new KubernetesSecretsTokenAuthProvider(); kubernetesSecretsTokenAuthProvider.initialize(coreV1Api, null, (fd) -> "default"); Function.FunctionDetails funcDetails = Function.FunctionDetails.newBuilder().setTenant("test-tenant").setNamespace("test-ns").setName("test-func").build(); Optional<FunctionAuthData> functionAuthData = kubernetesSecretsTokenAuthProvider.cacheAuthData(funcDetails, new AuthenticationDataSource() { @Override public boolean hasDataFromCommand() { return true; } @Override public String getCommandData() { return "test-token"; } }); Assert.assertTrue(functionAuthData.isPresent()); Assert.assertTrue(StringUtils.isNotBlank(new String(functionAuthData.get().getData()))); }
@Override public Optional<FunctionAuthData> cacheAuthData(Function.FunctionDetails funcDetails, AuthenticationDataSource authenticationDataSource) { String id = null; String tenant = funcDetails.getTenant(); String namespace = funcDetails.getNamespace(); String name = funcDetails.getName(); try { String token = getToken(authenticationDataSource); if (token != null) { id = createSecret(token, funcDetails); } } catch (Exception e) { log.warn("Failed to get token for function {}", FunctionCommon.getFullyQualifiedName(tenant, namespace, name), e); } if (id != null) { return Optional.of(FunctionAuthData.builder().data(id.getBytes()).build()); } return Optional.empty(); }
KubernetesSecretsTokenAuthProvider implements KubernetesFunctionAuthProvider { @Override public Optional<FunctionAuthData> cacheAuthData(Function.FunctionDetails funcDetails, AuthenticationDataSource authenticationDataSource) { String id = null; String tenant = funcDetails.getTenant(); String namespace = funcDetails.getNamespace(); String name = funcDetails.getName(); try { String token = getToken(authenticationDataSource); if (token != null) { id = createSecret(token, funcDetails); } } catch (Exception e) { log.warn("Failed to get token for function {}", FunctionCommon.getFullyQualifiedName(tenant, namespace, name), e); } if (id != null) { return Optional.of(FunctionAuthData.builder().data(id.getBytes()).build()); } return Optional.empty(); } }
KubernetesSecretsTokenAuthProvider implements KubernetesFunctionAuthProvider { @Override public Optional<FunctionAuthData> cacheAuthData(Function.FunctionDetails funcDetails, AuthenticationDataSource authenticationDataSource) { String id = null; String tenant = funcDetails.getTenant(); String namespace = funcDetails.getNamespace(); String name = funcDetails.getName(); try { String token = getToken(authenticationDataSource); if (token != null) { id = createSecret(token, funcDetails); } } catch (Exception e) { log.warn("Failed to get token for function {}", FunctionCommon.getFullyQualifiedName(tenant, namespace, name), e); } if (id != null) { return Optional.of(FunctionAuthData.builder().data(id.getBytes()).build()); } return Optional.empty(); }  }
KubernetesSecretsTokenAuthProvider implements KubernetesFunctionAuthProvider { @Override public Optional<FunctionAuthData> cacheAuthData(Function.FunctionDetails funcDetails, AuthenticationDataSource authenticationDataSource) { String id = null; String tenant = funcDetails.getTenant(); String namespace = funcDetails.getNamespace(); String name = funcDetails.getName(); try { String token = getToken(authenticationDataSource); if (token != null) { id = createSecret(token, funcDetails); } } catch (Exception e) { log.warn("Failed to get token for function {}", FunctionCommon.getFullyQualifiedName(tenant, namespace, name), e); } if (id != null) { return Optional.of(FunctionAuthData.builder().data(id.getBytes()).build()); } return Optional.empty(); }  @Override void initialize(CoreV1Api coreClient); @Override void setCaBytes(byte[] caBytes); @Override void setNamespaceProviderFunc(java.util.function.Function<Function.FunctionDetails, String> getNamespaceFromDetails); @Override void configureAuthDataStatefulSet(V1StatefulSet statefulSet, Optional<FunctionAuthData> functionAuthData); @Override void configureAuthenticationConfig(AuthenticationConfig authConfig, Optional<FunctionAuthData> functionAuthData); @Override Optional<FunctionAuthData> cacheAuthData(Function.FunctionDetails funcDetails,
                                                    AuthenticationDataSource authenticationDataSource); @Override void cleanUpAuthData(Function.FunctionDetails funcDetails, Optional<FunctionAuthData> functionAuthData); @Override Optional<FunctionAuthData> updateAuthData(Function.FunctionDetails funcDetails,
                                                     Optional<FunctionAuthData> existingFunctionAuthData,
                                                     AuthenticationDataSource authenticationDataSource); }
KubernetesSecretsTokenAuthProvider implements KubernetesFunctionAuthProvider { @Override public Optional<FunctionAuthData> cacheAuthData(Function.FunctionDetails funcDetails, AuthenticationDataSource authenticationDataSource) { String id = null; String tenant = funcDetails.getTenant(); String namespace = funcDetails.getNamespace(); String name = funcDetails.getName(); try { String token = getToken(authenticationDataSource); if (token != null) { id = createSecret(token, funcDetails); } } catch (Exception e) { log.warn("Failed to get token for function {}", FunctionCommon.getFullyQualifiedName(tenant, namespace, name), e); } if (id != null) { return Optional.of(FunctionAuthData.builder().data(id.getBytes()).build()); } return Optional.empty(); }  @Override void initialize(CoreV1Api coreClient); @Override void setCaBytes(byte[] caBytes); @Override void setNamespaceProviderFunc(java.util.function.Function<Function.FunctionDetails, String> getNamespaceFromDetails); @Override void configureAuthDataStatefulSet(V1StatefulSet statefulSet, Optional<FunctionAuthData> functionAuthData); @Override void configureAuthenticationConfig(AuthenticationConfig authConfig, Optional<FunctionAuthData> functionAuthData); @Override Optional<FunctionAuthData> cacheAuthData(Function.FunctionDetails funcDetails,
                                                    AuthenticationDataSource authenticationDataSource); @Override void cleanUpAuthData(Function.FunctionDetails funcDetails, Optional<FunctionAuthData> functionAuthData); @Override Optional<FunctionAuthData> updateAuthData(Function.FunctionDetails funcDetails,
                                                     Optional<FunctionAuthData> existingFunctionAuthData,
                                                     AuthenticationDataSource authenticationDataSource);  }