Duplicate creating a git-credentials secret on token fetch (#551)

Duplicate the git-credentials secret creation step on create personal access token secret. Currently this step is performed on workspace provision step but in this case the PAT secret might be initialised when the provision is finished. In order to synchronise the personal access token secret and git credentials secret creation step duplicate the git credentials secret creation step after the PAT secret creation step.
Refactor the get(scmServerUrl) function in the KubernetesPersonalAccessTokenManager class
pull/553/head
Igor Vinokur 2023-09-05 12:49:09 +03:00 committed by GitHub
parent afd7cad8b8
commit da5174bf17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 24 additions and 29 deletions

View File

@ -232,15 +232,7 @@ public class KubernetesPersonalAccessTokenManager implements PersonalAccessToken
throws ScmCommunicationException, ScmConfigurationPersistenceException, throws ScmCommunicationException, ScmConfigurationPersistenceException,
UnknownScmProviderException, UnsatisfiedScmPreconditionException, UnknownScmProviderException, UnsatisfiedScmPreconditionException,
ScmUnauthorizedException { ScmUnauthorizedException {
Subject subject = EnvironmentContext.getCurrent().getSubject(); PersonalAccessToken personalAccessToken = get(scmServerUrl);
Optional<PersonalAccessToken> tokenOptional = get(subject, scmServerUrl);
PersonalAccessToken personalAccessToken;
if (tokenOptional.isPresent()) {
personalAccessToken = tokenOptional.get();
} else {
// try to authenticate for the given URL
personalAccessToken = fetchAndSave(subject, scmServerUrl);
}
gitCredentialManager.createOrReplace(personalAccessToken); gitCredentialManager.createOrReplace(personalAccessToken);
return personalAccessToken; return personalAccessToken;
} }

View File

@ -44,7 +44,7 @@ public class BitbucketServerAuthorizingFileContentProviderTest {
PersonalAccessToken token = PersonalAccessToken token =
new PersonalAccessToken(TEST_SCHEME + "://" + TEST_HOSTNAME, "user1", "token"); new PersonalAccessToken(TEST_SCHEME + "://" + TEST_HOSTNAME, "user1", "token");
when(personalAccessTokenManager.get(anyString())).thenReturn(token); when(personalAccessTokenManager.getAndStore(anyString())).thenReturn(token);
String fileURL = "https://foo.bar/scm/repo/.devfile"; String fileURL = "https://foo.bar/scm/repo/.devfile";
@ -65,7 +65,8 @@ public class BitbucketServerAuthorizingFileContentProviderTest {
PersonalAccessToken token = PersonalAccessToken token =
new PersonalAccessToken(TEST_SCHEME + "://" + TEST_HOSTNAME, "user1", "token"); new PersonalAccessToken(TEST_SCHEME + "://" + TEST_HOSTNAME, "user1", "token");
when(personalAccessTokenManager.get(eq(TEST_SCHEME + "://" + TEST_HOSTNAME))).thenReturn(token); when(personalAccessTokenManager.getAndStore(eq(TEST_SCHEME + "://" + TEST_HOSTNAME)))
.thenReturn(token);
String fileURL = "https://foo.bar/scm/repo/.devfile"; String fileURL = "https://foo.bar/scm/repo/.devfile";
@ -73,7 +74,7 @@ public class BitbucketServerAuthorizingFileContentProviderTest {
fileContentProvider.fetchContent(fileURL); fileContentProvider.fetchContent(fileURL);
// then // then
verify(personalAccessTokenManager).get(eq(TEST_SCHEME + "://" + TEST_HOSTNAME)); verify(personalAccessTokenManager).getAndStore(eq(TEST_SCHEME + "://" + TEST_HOSTNAME));
verify(urlFetcher).fetch(eq(fileURL), eq("Bearer token")); verify(urlFetcher).fetch(eq(fileURL), eq("Bearer token"));
} }
@ -95,7 +96,7 @@ public class BitbucketServerAuthorizingFileContentProviderTest {
url, urlFetcher, personalAccessTokenManager); url, urlFetcher, personalAccessTokenManager);
PersonalAccessToken token = PersonalAccessToken token =
new PersonalAccessToken(TEST_SCHEME + "://" + TEST_HOSTNAME, "user1", "token"); new PersonalAccessToken(TEST_SCHEME + "://" + TEST_HOSTNAME, "user1", "token");
when(personalAccessTokenManager.get(anyString())).thenReturn(token); when(personalAccessTokenManager.getAndStore(anyString())).thenReturn(token);
// when // when
fileContentProvider.fetchContent(relative); fileContentProvider.fetchContent(relative);

View File

@ -73,7 +73,7 @@ public class BitbucketServerScmFileResolverTest {
public void shouldReturnContentFromUrlFetcher() throws Exception { public void shouldReturnContentFromUrlFetcher() throws Exception {
final String rawContent = "raw_content"; final String rawContent = "raw_content";
final String filename = "devfile.yaml"; final String filename = "devfile.yaml";
when(personalAccessTokenManager.get(anyString())) when(personalAccessTokenManager.getAndStore(anyString()))
.thenReturn(new PersonalAccessToken(SCM_URL, "root", "token123")); .thenReturn(new PersonalAccessToken(SCM_URL, "root", "token123"));
when(urlFetcher.fetch(anyString(), eq("Bearer token123"))).thenReturn(rawContent); when(urlFetcher.fetch(anyString(), eq("Bearer token123"))).thenReturn(rawContent);
@ -87,7 +87,7 @@ public class BitbucketServerScmFileResolverTest {
@Test @Test
public void shouldFetchContentWithoutAuthentication() throws Exception { public void shouldFetchContentWithoutAuthentication() throws Exception {
// given // given
when(personalAccessTokenManager.get(anyString())) when(personalAccessTokenManager.getAndStore(anyString()))
.thenThrow(new ScmUnauthorizedException("message", "bitbucket-server", "v1", "url")); .thenThrow(new ScmUnauthorizedException("message", "bitbucket-server", "v1", "url"));
// when // when

View File

@ -54,7 +54,7 @@ public class GithubAuthorizingFileContentProviderTest {
FileContentProvider fileContentProvider = FileContentProvider fileContentProvider =
new GithubAuthorizingFileContentProvider(githubUrl, urlFetcher, personalAccessTokenManager); new GithubAuthorizingFileContentProvider(githubUrl, urlFetcher, personalAccessTokenManager);
when(personalAccessTokenManager.get(anyString())) when(personalAccessTokenManager.getAndStore(anyString()))
.thenReturn(new PersonalAccessToken("foo", "che", "my-token")); .thenReturn(new PersonalAccessToken("foo", "che", "my-token"));
fileContentProvider.fetchContent("devfile.yaml"); fileContentProvider.fetchContent("devfile.yaml");
@ -81,7 +81,7 @@ public class GithubAuthorizingFileContentProviderTest {
FileContentProvider fileContentProvider = FileContentProvider fileContentProvider =
new GithubAuthorizingFileContentProvider(githubUrl, urlFetcher, personalAccessTokenManager); new GithubAuthorizingFileContentProvider(githubUrl, urlFetcher, personalAccessTokenManager);
when(personalAccessTokenManager.get(anyString())) when(personalAccessTokenManager.getAndStore(anyString()))
.thenReturn(new PersonalAccessToken(raw_url, "che", "my-token")); .thenReturn(new PersonalAccessToken(raw_url, "che", "my-token"));
fileContentProvider.fetchContent(raw_url); fileContentProvider.fetchContent(raw_url);
@ -98,7 +98,8 @@ public class GithubAuthorizingFileContentProviderTest {
FileContentProvider fileContentProvider = FileContentProvider fileContentProvider =
new GithubAuthorizingFileContentProvider(githubUrl, urlFetcher, personalAccessTokenManager); new GithubAuthorizingFileContentProvider(githubUrl, urlFetcher, personalAccessTokenManager);
when(personalAccessTokenManager.get(anyString())).thenThrow(UnknownScmProviderException.class); when(personalAccessTokenManager.getAndStore(anyString()))
.thenThrow(UnknownScmProviderException.class);
when(urlFetcher.fetch(eq(url))).thenThrow(FileNotFoundException.class); when(urlFetcher.fetch(eq(url))).thenThrow(FileNotFoundException.class);
@ -114,7 +115,8 @@ public class GithubAuthorizingFileContentProviderTest {
FileContentProvider fileContentProvider = FileContentProvider fileContentProvider =
new GithubAuthorizingFileContentProvider(githubUrl, urlFetcher, personalAccessTokenManager); new GithubAuthorizingFileContentProvider(githubUrl, urlFetcher, personalAccessTokenManager);
when(personalAccessTokenManager.get(anyString())).thenThrow(UnknownScmProviderException.class); when(personalAccessTokenManager.getAndStore(anyString()))
.thenThrow(UnknownScmProviderException.class);
when(urlFetcher.fetch(eq(url))).thenThrow(FileNotFoundException.class); when(urlFetcher.fetch(eq(url))).thenThrow(FileNotFoundException.class);
when(urlFetcher.fetch(eq("https://github.com/eclipse/che"))).thenThrow(IOException.class); when(urlFetcher.fetch(eq("https://github.com/eclipse/che"))).thenThrow(IOException.class);
@ -130,7 +132,7 @@ public class GithubAuthorizingFileContentProviderTest {
FileContentProvider fileContentProvider = FileContentProvider fileContentProvider =
new GithubAuthorizingFileContentProvider(githubUrl, urlFetcher, personalAccessTokenManager); new GithubAuthorizingFileContentProvider(githubUrl, urlFetcher, personalAccessTokenManager);
var personalAccessToken = new PersonalAccessToken(raw_url, "che", "my-token"); var personalAccessToken = new PersonalAccessToken(raw_url, "che", "my-token");
when(personalAccessTokenManager.get(anyString())).thenReturn(personalAccessToken); when(personalAccessTokenManager.getAndStore(anyString())).thenReturn(personalAccessToken);
fileContentProvider.fetchContent(raw_url); fileContentProvider.fetchContent(raw_url);

View File

@ -87,7 +87,7 @@ public class GithubScmFileResolverTest {
.thenReturn(rawContent); .thenReturn(rawContent);
lenient() lenient()
.when(personalAccessTokenManager.get(anyString())) .when(personalAccessTokenManager.getAndStore(anyString()))
.thenReturn(new PersonalAccessToken("foo", "che", "my-token")); .thenReturn(new PersonalAccessToken("foo", "che", "my-token"));
when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any())) when(githubApiClient.getLatestCommit(anyString(), anyString(), anyString(), any()))
@ -106,7 +106,7 @@ public class GithubScmFileResolverTest {
public void shouldReturnContentWithoutAuthentication() throws Exception { public void shouldReturnContentWithoutAuthentication() throws Exception {
// given // given
lenient() lenient()
.when(personalAccessTokenManager.get(anyString())) .when(personalAccessTokenManager.getAndStore(anyString()))
.thenThrow(new ScmUnauthorizedException("message", "github", "v1", "url")); .thenThrow(new ScmUnauthorizedException("message", "github", "v1", "url"));
// when // when

View File

@ -36,7 +36,7 @@ public class GitlabAuthorizingFileContentProviderTest {
FileContentProvider fileContentProvider = FileContentProvider fileContentProvider =
new GitlabAuthorizingFileContentProvider(gitlabUrl, urlFetcher, personalAccessTokenManager); new GitlabAuthorizingFileContentProvider(gitlabUrl, urlFetcher, personalAccessTokenManager);
var personalAccessToken = new PersonalAccessToken("foo", "che", "my-token"); var personalAccessToken = new PersonalAccessToken("foo", "che", "my-token");
when(personalAccessTokenManager.get(anyString())).thenReturn(personalAccessToken); when(personalAccessTokenManager.getAndStore(anyString())).thenReturn(personalAccessToken);
fileContentProvider.fetchContent("devfile.yaml"); fileContentProvider.fetchContent("devfile.yaml");
verify(urlFetcher) verify(urlFetcher)
.fetch( .fetch(
@ -54,7 +54,7 @@ public class GitlabAuthorizingFileContentProviderTest {
String url = String url =
"https://gitlab.net/api/v4/projects/eclipse%2Fche/repository/files/devfile.yaml/raw"; "https://gitlab.net/api/v4/projects/eclipse%2Fche/repository/files/devfile.yaml/raw";
var personalAccessToken = new PersonalAccessToken(url, "che", "my-token"); var personalAccessToken = new PersonalAccessToken(url, "che", "my-token");
when(personalAccessTokenManager.get(anyString())).thenReturn(personalAccessToken); when(personalAccessTokenManager.getAndStore(anyString())).thenReturn(personalAccessToken);
fileContentProvider.fetchContent(url); fileContentProvider.fetchContent(url);
verify(urlFetcher).fetch(eq(url), eq("Bearer my-token")); verify(urlFetcher).fetch(eq(url), eq("Bearer my-token"));

View File

@ -72,7 +72,7 @@ public class GitlabScmFileResolverTest {
public void shouldReturnContentFromUrlFetcher() throws Exception { public void shouldReturnContentFromUrlFetcher() throws Exception {
final String rawContent = "raw_content"; final String rawContent = "raw_content";
final String filename = "devfile.yaml"; final String filename = "devfile.yaml";
when(personalAccessTokenManager.get(any(String.class))) when(personalAccessTokenManager.getAndStore(any(String.class)))
.thenReturn(new PersonalAccessToken(SCM_URL, "root", "token123")); .thenReturn(new PersonalAccessToken(SCM_URL, "root", "token123"));
when(urlFetcher.fetch(anyString(), eq("Bearer token123"))).thenReturn(rawContent); when(urlFetcher.fetch(anyString(), eq("Bearer token123"))).thenReturn(rawContent);
@ -86,7 +86,7 @@ public class GitlabScmFileResolverTest {
@Test @Test
public void shouldFetchContentWithoutAuthentication() throws Exception { public void shouldFetchContentWithoutAuthentication() throws Exception {
// given // given
when(personalAccessTokenManager.get(anyString())) when(personalAccessTokenManager.getAndStore(anyString()))
.thenThrow(new ScmUnauthorizedException("message", "gitlab", "v1", "url")); .thenThrow(new ScmUnauthorizedException("message", "gitlab", "v1", "url"));
// when // when

View File

@ -80,7 +80,7 @@ public class AuthorizingFileContentProvider<T extends RemoteFactoryUrl>
String authorization; String authorization;
if (isNullOrEmpty(credentials)) { if (isNullOrEmpty(credentials)) {
PersonalAccessToken token = PersonalAccessToken token =
personalAccessTokenManager.get(remoteFactoryUrl.getProviderUrl()); personalAccessTokenManager.getAndStore(remoteFactoryUrl.getProviderUrl());
authorization = authorization =
formatAuthorization( formatAuthorization(
token.getToken(), token.getToken(),

View File

@ -48,13 +48,13 @@ public class AuthorizingFactoryParameterResolverTest {
// given // given
when(remoteFactoryUrl.getProviderUrl()).thenReturn("https://provider.url"); when(remoteFactoryUrl.getProviderUrl()).thenReturn("https://provider.url");
when(urlFetcher.fetch(anyString(), anyString())).thenReturn("content"); when(urlFetcher.fetch(anyString(), anyString())).thenReturn("content");
when(personalAccessTokenManager.get(anyString())).thenReturn(personalAccessToken); when(personalAccessTokenManager.getAndStore(anyString())).thenReturn(personalAccessToken);
// when // when
provider.fetchContent("url"); provider.fetchContent("url");
// then // then
verify(personalAccessTokenManager).get(anyString()); verify(personalAccessTokenManager).getAndStore(anyString());
} }
@Test @Test