forked from line/centraldogma
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare PerPermission Updates for line#1060 Compatibility
Motivation To ensure smooth compatibility with line#1060, the `PerRolePermissions` and `RepositoryMetadata` structure need an update to support changes. Modifications: - Enhanced the `PerRolePermissions` and `RepositoryMetadata` deserializers to accept both an array or permissions and a permission. - Added `REPO_ADMIN` `Permission`. - Removed `MetadataApiService.updateSpecificUserPermission` and `updateSpecificTokenPermission`. - They are not used in the UI and we don't even have test cases for that. - Will add those APIs later when we need it. Result: - The `PerRolePermissions` and `RepositoryMetadata` structures now support the upcoming changes in line#1060.
- Loading branch information
Showing
7 changed files
with
240 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...c/main/java/com/linecorp/centraldogma/server/metadata/PerRolePermissionsDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you 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.linecorp.centraldogma.server.metadata; | ||
|
||
import java.io.IOException; | ||
import java.util.Set; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.google.common.collect.ImmutableSet; | ||
|
||
import com.linecorp.centraldogma.internal.Jackson; | ||
|
||
final class PerRolePermissionsDeserializer extends StdDeserializer<PerRolePermissions> { | ||
|
||
private static final long serialVersionUID = 1173216371065909688L; | ||
|
||
private static final TypeReference<Set<Permission>> PERMISSION_SET_TYPE = | ||
new TypeReference<Set<Permission>>() {}; | ||
|
||
PerRolePermissionsDeserializer() { | ||
super(PerRolePermissions.class); | ||
} | ||
|
||
@Override | ||
public PerRolePermissions deserialize(JsonParser p, DeserializationContext ctxt) | ||
throws IOException { | ||
final JsonNode jsonNode = p.readValueAsTree(); | ||
final Set<Permission> ownerPermission = getPermission(jsonNode.get("owner")); | ||
final Set<Permission> memberPermission = getPermission(jsonNode.get("member")); | ||
final Set<Permission> guestPermission = getPermission(jsonNode.get("guest")); | ||
|
||
return new PerRolePermissions(ownerPermission, memberPermission, guestPermission, null); | ||
} | ||
|
||
static Set<Permission> getPermission(@Nullable JsonNode jsonNode) { | ||
if (jsonNode == null || jsonNode.isNull()) { | ||
return ImmutableSet.of(); | ||
} | ||
if (jsonNode.isArray()) { | ||
// legacy format. e.g. [], ["READ"] or ["READ", "WRITE"] | ||
return Jackson.convertValue(jsonNode, PERMISSION_SET_TYPE); | ||
} | ||
// e.g. "READ", "WRITE" or "REPO_ADMIN" | ||
final Permission permission = Permission.valueOf(jsonNode.textValue()); | ||
if (permission == Permission.READ) { | ||
return ImmutableSet.of(Permission.READ); | ||
} | ||
// In this legacy format, REPO_ADMIN is the same as WRITE. | ||
return ImmutableSet.of(Permission.READ, Permission.WRITE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...c/main/java/com/linecorp/centraldogma/server/metadata/RepositoryMetadataDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you 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.linecorp.centraldogma.server.metadata; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; | ||
import com.google.common.collect.ImmutableMap; | ||
|
||
import com.linecorp.centraldogma.internal.Jackson; | ||
import com.linecorp.centraldogma.server.QuotaConfig; | ||
|
||
final class RepositoryMetadataDeserializer extends StdDeserializer<RepositoryMetadata> { | ||
|
||
private static final long serialVersionUID = 1173216371065909688L; | ||
|
||
RepositoryMetadataDeserializer() { | ||
super(RepositoryMetadata.class); | ||
} | ||
|
||
@Override | ||
public RepositoryMetadata deserialize(JsonParser p, DeserializationContext ctxt) | ||
throws IOException { | ||
final JsonNode jsonNode = p.readValueAsTree(); | ||
final String name = jsonNode.get("name").textValue(); | ||
final PerRolePermissions perRolePermissions = | ||
Jackson.treeToValue(jsonNode.get("perRolePermissions"), PerRolePermissions.class); | ||
final Map<String, Collection<Permission>> perUserPermissions = | ||
perPermission(jsonNode, "perUserPermissions"); | ||
final Map<String, Collection<Permission>> perTokenPermissions = | ||
perPermission(jsonNode, "perTokenPermissions"); | ||
final UserAndTimestamp creation = Jackson.treeToValue(jsonNode.get("creation"), UserAndTimestamp.class); | ||
final JsonNode removalNode = jsonNode.get("removal"); | ||
final UserAndTimestamp removal = | ||
removalNode == null ? null : Jackson.treeToValue(removalNode, UserAndTimestamp.class); | ||
|
||
final JsonNode writeQuotaNode = jsonNode.get("writeQuota"); | ||
final QuotaConfig writeQuota = | ||
writeQuotaNode == null ? null : Jackson.treeToValue(writeQuotaNode, QuotaConfig.class); | ||
|
||
return new RepositoryMetadata(name, perRolePermissions, perUserPermissions, perTokenPermissions, | ||
creation, removal, writeQuota); | ||
} | ||
|
||
private static Map<String, Collection<Permission>> perPermission(JsonNode rootNode, String filed) { | ||
final JsonNode permissionsNode = rootNode.get(filed); | ||
|
||
final ImmutableMap.Builder<String, Collection<Permission>> builder = ImmutableMap.builder(); | ||
final Iterator<Entry<String, JsonNode>> fields = permissionsNode.fields(); | ||
while (fields.hasNext()) { | ||
final Entry<String, JsonNode> field = fields.next(); | ||
final String id = field.getKey(); | ||
builder.put(id, PerRolePermissionsDeserializer.getPermission(field.getValue())); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
server/src/test/java/com/linecorp/centraldogma/server/metadata/PerRolePermissionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you 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.linecorp.centraldogma.server.metadata; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.linecorp.centraldogma.internal.Jackson; | ||
|
||
class PerRolePermissionsTest { | ||
|
||
@Test | ||
void deserialize() throws Exception { | ||
final String oldFormat = '{' + | ||
" \"owner\": [" + | ||
" \"READ\"," + | ||
" \"WRITE\"" + | ||
" ]," + | ||
" \"member\": [" + | ||
" \"READ\"," + | ||
" \"WRITE\"" + | ||
" ]," + | ||
" \"guest\": [" + | ||
" \"READ\"" + | ||
" ]," + | ||
" \"anonymous\": []" + | ||
'}'; | ||
final PerRolePermissions oldFormatPermission = Jackson.readValue(oldFormat, PerRolePermissions.class); | ||
assertThat(oldFormatPermission.member()).containsExactlyInAnyOrder(Permission.READ, Permission.WRITE); | ||
assertThat(oldFormatPermission.guest()).containsExactly(Permission.READ); | ||
|
||
final String newFormat = '{' + | ||
" \"member\": \"WRITE\"," + | ||
" \"guest\": \"READ\"" + | ||
'}'; | ||
final PerRolePermissions newFormatPermission = Jackson.readValue(newFormat, PerRolePermissions.class); | ||
assertThat(newFormatPermission.member()).containsExactlyInAnyOrder(Permission.READ, Permission.WRITE); | ||
assertThat(newFormatPermission.guest()).containsExactly(Permission.READ); | ||
|
||
final String newFormat2 = '{' + | ||
" \"member\": null," + | ||
" \"guest\": null" + | ||
'}'; | ||
final PerRolePermissions newFormatPermission2 = Jackson.readValue(newFormat2, PerRolePermissions.class); | ||
assertThat(newFormatPermission2.member()).isEmpty(); | ||
assertThat(newFormatPermission2.guest()).isEmpty(); | ||
|
||
final String mixed = '{' + | ||
" \"owner\": [" + | ||
" \"READ\"," + | ||
" \"WRITE\"" + | ||
" ]," + | ||
" \"member\": [" + | ||
" \"READ\"," + | ||
" \"WRITE\"" + | ||
" ]," + | ||
" \"guest\": \"READ\"" + | ||
'}'; | ||
final PerRolePermissions mixedFormatPermission = Jackson.readValue(mixed, PerRolePermissions.class); | ||
assertThat(mixedFormatPermission.member()).containsExactlyInAnyOrder(Permission.READ, Permission.WRITE); | ||
assertThat(mixedFormatPermission.guest()).containsExactly(Permission.READ); | ||
} | ||
} |