Fix behavior for unexisting files for public repositories (#243)

Signed-off-by: Max Shaposhnik <mshaposh@redhat.com>
pull/245/head
Max Shaposhnik 2022-01-17 17:09:48 +02:00 committed by GitHub
parent 8de18ae530
commit d6cce806ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2022 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@ -70,8 +70,8 @@ public class GitHubOAuthAuthenticatorProvider implements Provider<OAuthAuthentic
&& !isNullOrEmpty(tokenUri)
&& Objects.nonNull(redirectUris)
&& redirectUris.length != 0) {
String clientId = Files.readString(Path.of(clientIdPath));
String clientSecret = Files.readString(Path.of(clientSecretPath));
final String clientId = Files.readString(Path.of(clientIdPath)).trim();
final String clientSecret = Files.readString(Path.of(clientSecretPath)).trim();
if (!isNullOrEmpty(clientId) && !isNullOrEmpty(clientSecret)) {
return new GitHubOAuthAuthenticator(
clientId, clientSecret, redirectUris, authUri, tokenUri);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2022 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@ -11,6 +11,7 @@
*/
package org.eclipse.che.api.factory.server.github;
import java.io.IOException;
import org.eclipse.che.api.factory.server.scm.AuthorizingFileContentProvider;
import org.eclipse.che.api.factory.server.scm.GitCredentialManager;
import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager;
@ -37,4 +38,19 @@ class GithubAuthorizingFileContentProvider extends AuthorizingFileContentProvide
protected String formatAuthorization(String token) {
return "token " + token;
}
@Override
protected boolean isPublicRepository(GithubUrl remoteFactoryUrl) {
try {
urlFetcher.fetch(
GithubApiClient.GITHUB_API_SERVER
+ "/repos/"
+ remoteFactoryUrl.getUsername()
+ "/"
+ remoteFactoryUrl.getRepository());
return true;
} catch (IOException e) {
return false;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2022 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@ -13,7 +13,9 @@ package org.eclipse.che.api.factory.server.github;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.FileNotFoundException;
import org.eclipse.che.api.factory.server.scm.GitCredentialManager;
import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager;
import org.eclipse.che.api.workspace.server.devfile.FileContentProvider;
@ -53,4 +55,17 @@ public class GithubAuthorizingFileContentProviderTest {
fileContentProvider.fetchContent(url);
verify(urlFetcher).fetch(eq(url));
}
@Test(expectedExceptions = FileNotFoundException.class)
public void shouldThrowNotFoundForPublicRepos() throws Exception {
URLFetcher urlFetcher = Mockito.mock(URLFetcher.class);
String url = "https://raw.githubusercontent.com/foo/bar/devfile.yaml";
when(urlFetcher.fetch(eq(url))).thenThrow(FileNotFoundException.class);
when(urlFetcher.fetch(eq("https://api.github.com/repos/eclipse/che"))).thenReturn("OK");
GithubUrl githubUrl = new GithubUrl().withUsername("eclipse").withRepository("che");
FileContentProvider fileContentProvider =
new GithubAuthorizingFileContentProvider(
githubUrl, urlFetcher, gitCredentialManager, personalAccessTokenManager);
fileContentProvider.fetchContent(url);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2022 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@ -11,6 +11,7 @@
*/
package org.eclipse.che.api.factory.server.scm;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
@ -35,9 +36,9 @@ public class AuthorizingFileContentProvider<T extends RemoteFactoryUrl>
implements FileContentProvider {
private final T remoteFactoryUrl;
private final URLFetcher urlFetcher;
private final PersonalAccessTokenManager personalAccessTokenManager;
private final GitCredentialManager gitCredentialManager;
protected final URLFetcher urlFetcher;
public AuthorizingFileContentProvider(
T remoteFactoryUrl,
@ -74,6 +75,11 @@ public class AuthorizingFileContentProvider<T extends RemoteFactoryUrl>
"Failed to fetch a content from URL %s due to TLS key misconfiguration. Please refer to the docs about how to correctly import it. ",
requestURL));
throw new DevfileException(exception.getMessage(), cause);
} else if (exception instanceof FileNotFoundException) {
if (isPublicRepository(remoteFactoryUrl)) {
// for public repo-s return 404 as-is
throw exception;
}
}
// unable to determine exact cause, so let's just try to authorize...
try {
@ -109,6 +115,10 @@ public class AuthorizingFileContentProvider<T extends RemoteFactoryUrl>
}
}
protected boolean isPublicRepository(T remoteFactoryUrl) {
return false;
}
private String formatUrl(String fileURL) throws DevfileException {
String requestURL;
try {