Skip to content

Commit

Permalink
[ACS-9036] Improved licence upload error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cezary-witkowski committed Nov 25, 2024
1 parent 471e4dd commit bdc2f93
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import static org.alfresco.elasticsearch.upgrade.Utils.waitFor;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
Expand Down Expand Up @@ -140,11 +141,16 @@ public void setElasticsearchSearchService() throws IOException
repoHttpClient.setSearchService("elasticsearch");
}

public boolean uploadLicence(String licencePath)
public void tryToUploadLicence(String licencePath)
{
try
{
return repoHttpClient.uploadLicense(licencePath);
File licence = new File(licencePath);
if (!licence.isFile() || !licence.canRead())
{
throw new RuntimeException("Licence file at: %s is not a file or is not readable".formatted(licencePath));

Check warning

Code scanning / PMD

Avoid throwing raw exception types. Warning test

Avoid throwing raw exception types.
}
repoHttpClient.uploadLicense(licence);
}
catch (IOException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.alfresco.elasticsearch.upgrade.Utils.createTempContentStoreDirectory;

import java.nio.file.Path;
import java.util.Optional;

import org.testcontainers.containers.Network;

Expand Down Expand Up @@ -72,15 +73,13 @@ public ACSEnv upgradeLegacyEnvironmentToCurrent()

private void uploadLicence(ACSEnv env)
{
final String licencePath = getTargetAcsLicencePath();
if (!env.uploadLicence(licencePath))
{
throw new RuntimeException("Failed to upload licence from `" + licencePath + "`.");
}
String licencePath = getTargetAcsLicencePath();
env.tryToUploadLicence(licencePath);
}

private String getTargetAcsLicencePath()
{
return System.getenv("ALF_LICENCE_LOCAL_PATH");
return Optional.ofNullable(System.getenv("ALF_LICENCE_LOCAL_PATH"))
.orElseThrow(() -> new IllegalStateException("ALF_LICENCE_LOCAL_PATH environment variable is not set"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void setSearchService(String implementation) throws IOException
}
}

public boolean uploadLicense(String licensePath) throws IOException
public void uploadLicense(File license) throws IOException
{
final HttpGet getCsrfToken = authenticate(new HttpGet(uploadLicenseAdminApiUri));
final HttpClientContext httpCtx = HttpClientContext.create();
Expand All @@ -128,9 +128,6 @@ public boolean uploadLicense(String licensePath) throws IOException
csrfCookie = Objects.requireNonNull(cookies.get("alf-csrftoken"));
}


File license = new File(licensePath);

final URI uploadLicenseAdminCsrfApiUri = URI.create(new URIBuilder(uploadLicenseAdminApiUri)
.addParameter(csrfCookie.getName(), URLDecoder.decode(csrfCookie.getValue(), StandardCharsets.US_ASCII))
.toString());
Expand All @@ -144,12 +141,11 @@ public boolean uploadLicense(String licensePath) throws IOException
final HttpPost uploadRequest = authenticate(new HttpPost(uploadLicenseAdminCsrfApiUri));
uploadRequest.setEntity(uploadEntity);

try (CloseableHttpResponse response = client.execute(uploadRequest, httpCtx))
var responseMap = executeAndGetResponseMap(uploadRequest, httpCtx);
if (!Boolean.TRUE.equals(responseMap.get("success")))
{
String responseBody = EntityUtils.toString(response.getEntity());
return gson.fromJson(responseBody, Map.class).get("success").equals(true);
throw new IOException("Failed to upload a licence. Server response error: " + responseMap.get("error"));
}

}

public boolean isServerUp() throws IOException
Expand Down Expand Up @@ -217,6 +213,15 @@ public Optional<Set<String>> searchForFiles(String term) throws IOException
return Optional.of(names);
}

private Map<?,?> executeAndGetResponseMap(HttpPost httpPost, HttpClientContext httpCtx) throws IOException
{
try (CloseableHttpResponse response = client.execute(httpPost, httpCtx))
{
String responseBody = EntityUtils.toString(response.getEntity());
return gson.fromJson(responseBody, Map.class);
}
}

private <T extends HttpMessage> T authenticate(T msg)
{
msg.setHeader("Authorization", "Basic YWRtaW46YWRtaW4=");
Expand Down

0 comments on commit bdc2f93

Please sign in to comment.