@Test void testPersistKubeConfigWithUpdatedToken() throws IOException { Map<String, Object> openIdProviderResponse = new HashMap<>(); openIdProviderResponse.put(ID_TOKEN_PARAM, "id-token-updated"); openIdProviderResponse.put(ACCESS_TOKEN_PARAM, "refresh-token-updated"); File tempFile = Files.createTempFile("test", "kubeconfig").toFile(); Files.copy(getClass().getResourceAsStream("/test-kubeconfig-oidc"), Paths.get(tempFile.getPath()), StandardCopyOption.REPLACE_EXISTING); boolean isPersisted = OpenIDConnectionUtils.persistKubeConfigWithUpdatedToken(tempFile.getAbsolutePath(), openIdProviderResponse); assertTrue(isPersisted); io.fabric8.kubernetes.api.model.Config config = KubeConfigUtils.parseConfig(tempFile); assertNotNull(config); NamedContext currentNamedContext = KubeConfigUtils.getCurrentContext(config); assertNotNull(currentNamedContext); int currentUserIndex = KubeConfigUtils.getNamedUserIndexFromConfig(config, currentNamedContext.getContext().getUser()); assertTrue(currentUserIndex > 0); Map<String, String> authProviderConfig = config.getUsers().get(currentUserIndex).getUser().getAuthProvider().getConfig(); assertFalse(authProviderConfig.isEmpty()); assertEquals("id-token-updated", authProviderConfig.get(ID_TOKEN_KUBECONFIG)); assertEquals("refresh-token-updated", authProviderConfig.get(REFRESH_TOKEN_KUBECONFIG)); }
static boolean persistKubeConfigWithUpdatedToken(String kubeConfigPath, Map<String, Object> updatedAuthProviderConfig) throws IOException { io.fabric8.kubernetes.api.model.Config config = KubeConfigUtils.parseConfig(new File(kubeConfigPath)); NamedContext currentNamedContext = KubeConfigUtils.getCurrentContext(config); if (currentNamedContext != null) { int currentUserIndex = KubeConfigUtils.getNamedUserIndexFromConfig(config, currentNamedContext.getContext().getUser()); Map<String, String> authProviderConfig = config.getUsers().get(currentUserIndex).getUser().getAuthProvider().getConfig(); authProviderConfig.put(ID_TOKEN_KUBECONFIG, String.valueOf(updatedAuthProviderConfig.get(ID_TOKEN_PARAM))); authProviderConfig.put(REFRESH_TOKEN_KUBECONFIG, String.valueOf(updatedAuthProviderConfig.get(ACCESS_TOKEN_PARAM))); config.getUsers().get(currentUserIndex).getUser().getAuthProvider().setConfig(authProviderConfig); try { KubeConfigUtils.persistKubeConfigIntoFile(config, kubeConfigPath); return true; } catch (IOException exception) { LOGGER.warn("failed to write file {}", kubeConfigPath, exception); } } return false; }
OpenIDConnectionUtils { static boolean persistKubeConfigWithUpdatedToken(String kubeConfigPath, Map<String, Object> updatedAuthProviderConfig) throws IOException { io.fabric8.kubernetes.api.model.Config config = KubeConfigUtils.parseConfig(new File(kubeConfigPath)); NamedContext currentNamedContext = KubeConfigUtils.getCurrentContext(config); if (currentNamedContext != null) { int currentUserIndex = KubeConfigUtils.getNamedUserIndexFromConfig(config, currentNamedContext.getContext().getUser()); Map<String, String> authProviderConfig = config.getUsers().get(currentUserIndex).getUser().getAuthProvider().getConfig(); authProviderConfig.put(ID_TOKEN_KUBECONFIG, String.valueOf(updatedAuthProviderConfig.get(ID_TOKEN_PARAM))); authProviderConfig.put(REFRESH_TOKEN_KUBECONFIG, String.valueOf(updatedAuthProviderConfig.get(ACCESS_TOKEN_PARAM))); config.getUsers().get(currentUserIndex).getUser().getAuthProvider().setConfig(authProviderConfig); try { KubeConfigUtils.persistKubeConfigIntoFile(config, kubeConfigPath); return true; } catch (IOException exception) { LOGGER.warn("failed to write file {}", kubeConfigPath, exception); } } return false; } }
OpenIDConnectionUtils { static boolean persistKubeConfigWithUpdatedToken(String kubeConfigPath, Map<String, Object> updatedAuthProviderConfig) throws IOException { io.fabric8.kubernetes.api.model.Config config = KubeConfigUtils.parseConfig(new File(kubeConfigPath)); NamedContext currentNamedContext = KubeConfigUtils.getCurrentContext(config); if (currentNamedContext != null) { int currentUserIndex = KubeConfigUtils.getNamedUserIndexFromConfig(config, currentNamedContext.getContext().getUser()); Map<String, String> authProviderConfig = config.getUsers().get(currentUserIndex).getUser().getAuthProvider().getConfig(); authProviderConfig.put(ID_TOKEN_KUBECONFIG, String.valueOf(updatedAuthProviderConfig.get(ID_TOKEN_PARAM))); authProviderConfig.put(REFRESH_TOKEN_KUBECONFIG, String.valueOf(updatedAuthProviderConfig.get(ACCESS_TOKEN_PARAM))); config.getUsers().get(currentUserIndex).getUser().getAuthProvider().setConfig(authProviderConfig); try { KubeConfigUtils.persistKubeConfigIntoFile(config, kubeConfigPath); return true; } catch (IOException exception) { LOGGER.warn("failed to write file {}", kubeConfigPath, exception); } } return false; } private  OpenIDConnectionUtils(); }
OpenIDConnectionUtils { static boolean persistKubeConfigWithUpdatedToken(String kubeConfigPath, Map<String, Object> updatedAuthProviderConfig) throws IOException { io.fabric8.kubernetes.api.model.Config config = KubeConfigUtils.parseConfig(new File(kubeConfigPath)); NamedContext currentNamedContext = KubeConfigUtils.getCurrentContext(config); if (currentNamedContext != null) { int currentUserIndex = KubeConfigUtils.getNamedUserIndexFromConfig(config, currentNamedContext.getContext().getUser()); Map<String, String> authProviderConfig = config.getUsers().get(currentUserIndex).getUser().getAuthProvider().getConfig(); authProviderConfig.put(ID_TOKEN_KUBECONFIG, String.valueOf(updatedAuthProviderConfig.get(ID_TOKEN_PARAM))); authProviderConfig.put(REFRESH_TOKEN_KUBECONFIG, String.valueOf(updatedAuthProviderConfig.get(ACCESS_TOKEN_PARAM))); config.getUsers().get(currentUserIndex).getUser().getAuthProvider().setConfig(authProviderConfig); try { KubeConfigUtils.persistKubeConfigIntoFile(config, kubeConfigPath); return true; } catch (IOException exception) { LOGGER.warn("failed to write file {}", kubeConfigPath, exception); } } return false; } private  OpenIDConnectionUtils(); static String resolveOIDCTokenFromAuthConfig(Map<String, String> currentAuthProviderConfig); }
OpenIDConnectionUtils { static boolean persistKubeConfigWithUpdatedToken(String kubeConfigPath, Map<String, Object> updatedAuthProviderConfig) throws IOException { io.fabric8.kubernetes.api.model.Config config = KubeConfigUtils.parseConfig(new File(kubeConfigPath)); NamedContext currentNamedContext = KubeConfigUtils.getCurrentContext(config); if (currentNamedContext != null) { int currentUserIndex = KubeConfigUtils.getNamedUserIndexFromConfig(config, currentNamedContext.getContext().getUser()); Map<String, String> authProviderConfig = config.getUsers().get(currentUserIndex).getUser().getAuthProvider().getConfig(); authProviderConfig.put(ID_TOKEN_KUBECONFIG, String.valueOf(updatedAuthProviderConfig.get(ID_TOKEN_PARAM))); authProviderConfig.put(REFRESH_TOKEN_KUBECONFIG, String.valueOf(updatedAuthProviderConfig.get(ACCESS_TOKEN_PARAM))); config.getUsers().get(currentUserIndex).getUser().getAuthProvider().setConfig(authProviderConfig); try { KubeConfigUtils.persistKubeConfigIntoFile(config, kubeConfigPath); return true; } catch (IOException exception) { LOGGER.warn("failed to write file {}", kubeConfigPath, exception); } } return false; } private  OpenIDConnectionUtils(); static String resolveOIDCTokenFromAuthConfig(Map<String, String> currentAuthProviderConfig); static final String ID_TOKEN_KUBECONFIG; static final String ISSUER_KUBECONFIG; static final String REFRESH_TOKEN_KUBECONFIG; static final String REFRESH_TOKEN_PARAM; static final String GRANT_TYPE_PARAM; static final String CLIENT_ID_PARAM; static final String CLIENT_SECRET_PARAM; static final String ID_TOKEN_PARAM; static final String ACCESS_TOKEN_PARAM; static final String CLIENT_ID_KUBECONFIG; static final String CLIENT_SECRET_KUBECONFIG; static final String IDP_CERT_DATA; static final String TOKEN_ENDPOINT_PARAM; static final String WELL_KNOWN_OPENID_CONFIGURATION; static final String GRANT_TYPE_REFRESH_TOKEN; }