@Test public void testUpdateAuthData() throws Exception { CoreV1Api coreV1Api = mock(CoreV1Api.class); KubernetesSecretsTokenAuthProvider kubernetesSecretsTokenAuthProvider = new KubernetesSecretsTokenAuthProvider(); kubernetesSecretsTokenAuthProvider.initialize(coreV1Api, null, (fd) -> "default"); Optional<FunctionAuthData> existingFunctionAuthData = Optional.empty(); Function.FunctionDetails funcDetails = Function.FunctionDetails.newBuilder().setTenant("test-tenant").setNamespace("test-ns").setName("test-func").build(); Optional<FunctionAuthData> functionAuthData = kubernetesSecretsTokenAuthProvider.updateAuthData(funcDetails, existingFunctionAuthData, 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()))); existingFunctionAuthData = Optional.of(new FunctionAuthData("pf-secret-z7mxx".getBytes(), null)); functionAuthData = kubernetesSecretsTokenAuthProvider.updateAuthData(funcDetails, existingFunctionAuthData, new AuthenticationDataSource() { @Override public boolean hasDataFromCommand() { return true; } @Override public String getCommandData() { return "test-token"; } }); Assert.assertTrue(functionAuthData.isPresent()); Assert.assertEquals(new String(functionAuthData.get().getData()), "pf-secret-z7mxx"); }
@Override public Optional<FunctionAuthData> updateAuthData(Function.FunctionDetails funcDetails, Optional<FunctionAuthData> existingFunctionAuthData, AuthenticationDataSource authenticationDataSource) throws Exception { String secretId; secretId = existingFunctionAuthData.map(functionAuthData -> new String(functionAuthData.getData())).orElseGet(() -> RandomStringUtils.random(5, true, true).toLowerCase()); String token; try { token = getToken(authenticationDataSource); } catch (AuthenticationException e) { cleanUpAuthData( funcDetails, existingFunctionAuthData); return Optional.empty(); } if (token != null) { upsertSecret(token, funcDetails, getSecretName(secretId)); return Optional.of(FunctionAuthData.builder().data(secretId.getBytes()).build()); } return existingFunctionAuthData; }
KubernetesSecretsTokenAuthProvider implements KubernetesFunctionAuthProvider { @Override public Optional<FunctionAuthData> updateAuthData(Function.FunctionDetails funcDetails, Optional<FunctionAuthData> existingFunctionAuthData, AuthenticationDataSource authenticationDataSource) throws Exception { String secretId; secretId = existingFunctionAuthData.map(functionAuthData -> new String(functionAuthData.getData())).orElseGet(() -> RandomStringUtils.random(5, true, true).toLowerCase()); String token; try { token = getToken(authenticationDataSource); } catch (AuthenticationException e) { cleanUpAuthData( funcDetails, existingFunctionAuthData); return Optional.empty(); } if (token != null) { upsertSecret(token, funcDetails, getSecretName(secretId)); return Optional.of(FunctionAuthData.builder().data(secretId.getBytes()).build()); } return existingFunctionAuthData; } }
KubernetesSecretsTokenAuthProvider implements KubernetesFunctionAuthProvider { @Override public Optional<FunctionAuthData> updateAuthData(Function.FunctionDetails funcDetails, Optional<FunctionAuthData> existingFunctionAuthData, AuthenticationDataSource authenticationDataSource) throws Exception { String secretId; secretId = existingFunctionAuthData.map(functionAuthData -> new String(functionAuthData.getData())).orElseGet(() -> RandomStringUtils.random(5, true, true).toLowerCase()); String token; try { token = getToken(authenticationDataSource); } catch (AuthenticationException e) { cleanUpAuthData( funcDetails, existingFunctionAuthData); return Optional.empty(); } if (token != null) { upsertSecret(token, funcDetails, getSecretName(secretId)); return Optional.of(FunctionAuthData.builder().data(secretId.getBytes()).build()); } return existingFunctionAuthData; }  }
KubernetesSecretsTokenAuthProvider implements KubernetesFunctionAuthProvider { @Override public Optional<FunctionAuthData> updateAuthData(Function.FunctionDetails funcDetails, Optional<FunctionAuthData> existingFunctionAuthData, AuthenticationDataSource authenticationDataSource) throws Exception { String secretId; secretId = existingFunctionAuthData.map(functionAuthData -> new String(functionAuthData.getData())).orElseGet(() -> RandomStringUtils.random(5, true, true).toLowerCase()); String token; try { token = getToken(authenticationDataSource); } catch (AuthenticationException e) { cleanUpAuthData( funcDetails, existingFunctionAuthData); return Optional.empty(); } if (token != null) { upsertSecret(token, funcDetails, getSecretName(secretId)); return Optional.of(FunctionAuthData.builder().data(secretId.getBytes()).build()); } return existingFunctionAuthData; }  @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> updateAuthData(Function.FunctionDetails funcDetails, Optional<FunctionAuthData> existingFunctionAuthData, AuthenticationDataSource authenticationDataSource) throws Exception { String secretId; secretId = existingFunctionAuthData.map(functionAuthData -> new String(functionAuthData.getData())).orElseGet(() -> RandomStringUtils.random(5, true, true).toLowerCase()); String token; try { token = getToken(authenticationDataSource); } catch (AuthenticationException e) { cleanUpAuthData( funcDetails, existingFunctionAuthData); return Optional.empty(); } if (token != null) { upsertSecret(token, funcDetails, getSecretName(secretId)); return Optional.of(FunctionAuthData.builder().data(secretId.getBytes()).build()); } return existingFunctionAuthData; }  @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);  }