Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SO-5406 freeze retired resources #1007

Merged
merged 5 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,6 @@ public void dot() throws Exception {
.isEqualTo(branchName);
}

@Test
public void tilde() throws Exception {
String branchName = "~1.0";
assertBranchCreate(branchName)
.extracting(RevisionBranch::getName)
.isEqualTo(branchName);
}

@Test(expected = BadRequestException.class)
public void percent() throws Exception {
String branchName = "%xy";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static enum BranchState {
/**
* Allowed set of characters for a branch name.
*/
public static final String DEFAULT_ALLOWED_BRANCH_NAME_CHARACTER_SET = "a-zA-Z0-9.~_-";
public static final String DEFAULT_ALLOWED_BRANCH_NAME_CHARACTER_SET = "a-zA-Z0-9._-";

/**
* The maximum length of a branch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.iterableWithSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -40,6 +39,7 @@
import com.b2international.commons.exceptions.NotFoundException;
import com.b2international.commons.http.ExtendedLocale;
import com.b2international.commons.json.Json;
import com.b2international.snowowl.core.Resource;
import com.b2international.snowowl.core.ResourceURI;
import com.b2international.snowowl.core.branch.Branch;
import com.b2international.snowowl.core.codesystem.CodeSystem;
Expand Down Expand Up @@ -536,7 +536,7 @@ public void codesystem27_GetWithTimestamp() {
}

@Test
public void codeSystem28_SearchWithTimestamp() throws Exception {
public void codesystem28_SearchWithTimestamp() throws Exception {
assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs28_1"));
assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs28_2"));
assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs28_3"));
Expand All @@ -555,7 +555,7 @@ public void codeSystem28_SearchWithTimestamp() throws Exception {
}

@Test
public void codeSystem29_VersionWithReservedBranchName() throws Exception {
public void codesystem29_VersionWithReservedBranchName() throws Exception {
String codeSystemId = assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs29_1"));
assertVersionCreated(prepareVersionCreateRequestBody(CodeSystem.uri(codeSystemId), ResourceURI.HEAD, EffectiveTimes.today()))
.statusCode(400)
Expand All @@ -567,7 +567,35 @@ public void codeSystem29_VersionWithReservedBranchName() throws Exception {
.statusCode(400)
.body("message", containsString("Version 'NEXT' is a reserved alias or branch name."));
}


@Test
public void codesystem30_AllowMetadataUpdatesOnRetiredResources() throws Exception {
String codeSystemId = assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs30"));
assertCodeSystemUpdated(codeSystemId, Map.of(
"status", Resource.RETIRED_STATUS
));
assertCodeSystemGet(codeSystemId)
.statusCode(200)
.body("status", equalTo(Resource.RETIRED_STATUS));
assertCodeSystemUpdated(codeSystemId, Map.of(
"copyright", "MIT License"
));
assertCodeSystemGet(codeSystemId)
.statusCode(200)
.body("copyright", equalTo("MIT License"));
}

@Test
public void codesystem31_DisallowVersioningOfRetiredResources() throws Exception {
String codeSystemId = assertCodeSystemCreated(prepareCodeSystemCreateRequestBody("cs30"));
assertCodeSystemUpdated(codeSystemId, Map.of(
"status", Resource.RETIRED_STATUS
));
assertVersionCreated(prepareVersionCreateRequestBody(CodeSystem.uri(codeSystemId), "v1", EffectiveTimes.today()))
.statusCode(400)
.body("message", containsString("Resource 'Code System cs30' cannot be versioned in its current status 'retired'."));
}

private long getCodeSystemCreatedAt(final String id) {
return assertCodeSystemGet(id)
.statusCode(200)
Expand Down
2 changes: 1 addition & 1 deletion core/com.b2international.snowowl.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.9.0",
io.github.classgraph.classgraph;bundle-version="4.8.90",
com.b2international.groovy;bundle-version="3.0.9",
com.b2international.snomed.ecl;bundle-version="1.6.0";visibility:=reexport,
com.github.f4b6a3.uuid;bundle-version="4.6.1"
com.github.f4b6a3.uuid;bundle-version="4.6.1";visibility:=reexport
Bundle-RequiredExecutionEnvironment: JavaSE-17
Bundle-ActivationPolicy: lazy
Export-Package: com.auth0.jwt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public abstract class Resource implements Serializable {

private static final long serialVersionUID = 1L;

// known retired status value from FHIR, TODO make it configurable when needed
public static final String RETIRED_STATUS = "retired";

/**
* @since 8.0
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 B2i Healthcare Pte Ltd, https://b2i.sg
* Copyright 2021-2022 B2i Healthcare Pte Ltd, https://b2i.sg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*/
package com.b2international.snowowl.core.context;

import com.b2international.snowowl.core.Resource;
import com.b2international.snowowl.core.ResourceURI;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.TerminologyResource;
Expand All @@ -28,6 +29,7 @@ public final class DefaultTerminologyResourceContext extends DelegatingContext i
public DefaultTerminologyResourceContext(final ServiceProvider delegate, ResourceURI resourceUri, TerminologyResource resource) {
super(delegate);
bind(ResourceURI.class, resourceUri);
bind(Resource.class, resource);
bind(TerminologyResource.class, resource);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 B2i Healthcare Pte Ltd, https://b2i.sg
* Copyright 2021-2022 B2i Healthcare Pte Ltd, https://b2i.sg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,7 +53,8 @@ public TerminologyResourceRequest(final String toolingId, final String resourceP
this.toolingId = toolingId;
this.resourcePath = resourcePath;
if (resourcePath.startsWith(Branch.MAIN_PATH) && Strings.isNullOrEmpty(toolingId)) {
throw new BadRequestException("Reflective access ('repositoryId/path') to terminology resource content is not supported in this request builder.");
throw new BadRequestException("Reflective access ('repositoryId/path') to terminology resource content is not supported in this API.")
.withDeveloperMessage("No toolingId is specified on API level to ensure the correct reflective access to underlying terminology.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package com.b2international.snowowl.core.id;

import java.util.UUID;
import java.util.regex.Pattern;

import org.elasticsearch.common.UUIDs;

import com.b2international.commons.exceptions.BadRequestException;
import com.github.f4b6a3.uuid.codec.base.Base62Codec;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
Expand All @@ -30,6 +32,12 @@
*/
public class IDs {

public static final String BASE62_CHARSET = "A-Za-z0-9";
public static final String BASE64_CHARSET = "A-Za-z0-9-_";

public static final Pattern BASE62 = Pattern.compile("["+ BASE62_CHARSET +"]+");
public static final Pattern BASE64 = Pattern.compile("["+ BASE64_CHARSET +"]+");

/**
* Generates a time-based UUID (similar to Flake IDs), which is preferred when generating an ID to be indexed into a Lucene index as primary key. This methods uses Base 62 encoding of UUIDs to omit the usage of non-alpha/numeric characters entirely.
*
Expand Down Expand Up @@ -81,4 +89,18 @@ public static String sha1(String value) {
return Hashing.sha1().hashString(value, Charsets.UTF_8).toString();
}

public static void checkBase62(String value, String property) {
checkBaseN(value, property, BASE62, BASE62_CHARSET);
}

public static void checkBase64(String value, String property) {
checkBaseN(value, property, BASE64, BASE64_CHARSET);
}

private static void checkBaseN(String value, String property, Pattern pattern, String allowedCharacterSet) {
if (!pattern.matcher(value).matches()) {
throw new BadRequestException("%s'%s' uses an illegal character. Allowed characters are '%s'.", property == null ? "" : property.concat(" "), value, allowedCharacterSet);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
import org.hibernate.validator.constraints.NotEmpty;

import com.b2international.commons.exceptions.AlreadyExistsException;
import com.b2international.commons.exceptions.BadRequestException;
import com.b2international.commons.exceptions.NotFoundException;
import com.b2international.index.revision.RevisionBranch.BranchNameValidator;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.bundle.Bundle;
import com.b2international.snowowl.core.bundle.Bundles;
import com.b2international.snowowl.core.domain.IComponent;
import com.b2international.snowowl.core.domain.TransactionContext;
import com.b2international.snowowl.core.events.Request;
import com.b2international.snowowl.core.id.IDs;
import com.b2international.snowowl.core.identity.User;
import com.b2international.snowowl.core.internal.ResourceDocument;
import com.b2international.snowowl.core.internal.ResourceDocument.Builder;
Expand All @@ -43,7 +42,7 @@
public abstract class BaseResourceCreateRequest implements Request<TransactionContext, String> {

protected static final long serialVersionUID = 1L;

// the new ID, if not specified, it will be auto-generated
@JsonProperty
@NotEmpty
Expand Down Expand Up @@ -184,12 +183,8 @@ protected final void setPurpose(String purpose) {
@Override
public final String execute(TransactionContext context) {
// validate ID before use, IDs sometimes being used as branch paths, so must be a valid branch path
try {
context.service(BranchNameValidator.class).checkName(id);
} catch (BadRequestException e) {
throw new BadRequestException(e.getMessage().replace("Branch name", getClass().getSimpleName().replace("CreateRequest", ".id")));
}

IDs.checkBase64(id, getClass().getSimpleName().replace("CreateRequest", ".id"));

// validate URL format
getResourceURLSchemaSupport(context).validate(url);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 B2i Healthcare Pte Ltd, https://b2i.sg
* Copyright 2021-2022 B2i Healthcare Pte Ltd, https://b2i.sg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,12 @@
*/
package com.b2international.snowowl.core.request;

import java.util.Set;

import com.b2international.snowowl.core.Resource;
import com.b2international.snowowl.core.context.TerminologyResourceContentRequestBuilder;
import com.b2international.snowowl.core.domain.BranchContext;
import com.b2international.snowowl.core.events.Request;

/**
* @since 8.0
Expand All @@ -24,9 +29,16 @@ public class TerminologyResourceCommitRequestBuilder
extends RepositoryCommitRequestBuilder<TerminologyResourceCommitRequestBuilder>
implements TerminologyResourceContentRequestBuilder<CommitResult> {

public static final Set<String> READ_ONLY_STATUSES = Set.of(Resource.RETIRED_STATUS);

@Override
public boolean snapshot() {
return false;
}

@Override
public Request<BranchContext, CommitResult> wrap(Request<BranchContext, CommitResult> req) {
return new TerminologyResourceStatusCheckRequest<>(TerminologyResourceContentRequestBuilder.super.wrap(req), READ_ONLY_STATUSES);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022 B2i Healthcare Pte Ltd, https://b2i.sg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.b2international.snowowl.core.request;

import java.util.Collection;
import java.util.SortedSet;

import com.b2international.commons.exceptions.BadRequestException;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.TerminologyResource;
import com.b2international.snowowl.core.events.DelegatingRequest;
import com.b2international.snowowl.core.events.Request;
import com.google.common.collect.ImmutableSortedSet;

/**
* @since 8.2
*
* @param <C>
* @param <T>
* @param <R>
*/
public final class TerminologyResourceStatusCheckRequest<C extends ServiceProvider, R> extends DelegatingRequest<C, C, R> {

private static final long serialVersionUID = 1L;
private final SortedSet<String> forbiddenStatuses;

public TerminologyResourceStatusCheckRequest(Request<C, R> next, Collection<String> forbiddenStatuses) {
super(next);
this.forbiddenStatuses = forbiddenStatuses == null ? ImmutableSortedSet.of() : ImmutableSortedSet.copyOf(forbiddenStatuses);
}

@Override
public R execute(C context) {
TerminologyResource resource = context.service(TerminologyResource.class);

if (forbiddenStatuses.contains(resource.getStatus())) {
throw new BadRequestException("Executing this request is forbidden on resources with one of the following states '%s'. Resource '%s' is in '%s' status.", this.forbiddenStatuses, resource.getTitle(), resource.getStatus());
} else {
return next(context);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public Boolean execute(RepositoryContext context) {
context.service(BranchNameValidator.class).checkName(version);

TerminologyResource resourceToVersion = resourcesById.get(resource);

if (TerminologyResourceCommitRequestBuilder.READ_ONLY_STATUSES.contains(resourceToVersion.getStatus())) {
throw new BadRequestException("Resource '%s' cannot be versioned in its current status '%s'.", resourceToVersion.getTitle(), resourceToVersion.getStatus());
}

// TODO resurrect or eliminate tooling dependencies
final List<TerminologyResource> resourcesToVersion = List.of(resourcesById.get(resource));
Expand Down