-
Notifications
You must be signed in to change notification settings - Fork 121
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
Prepare RepositoryMetadata role Updates for #1060 Compatibility #1061
Changes from 6 commits
09a6593
3bc765b
4f2e577
140428b
5eea3d7
b19c106
a1f2967
2b59432
2ff378d
059a5f4
144d172
b096bad
c054553
f8d8aca
1fe2f94
fe31752
27b8044
2b2ca34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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 com.google.common.collect.ImmutableMap.toImmutableMap; | ||
import static com.linecorp.centraldogma.server.metadata.PerRolePermissions.READ_WRITE; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
|
||
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.ImmutableList; | ||
|
||
import com.linecorp.centraldogma.internal.Jackson; | ||
import com.linecorp.centraldogma.server.QuotaConfig; | ||
|
||
final class RepositoryMetadataDeserializer extends StdDeserializer<RepositoryMetadata> { | ||
|
||
private static final long serialVersionUID = 1173216371065909688L; | ||
|
||
private static final TypeReference<Map<String, Collection<Permission>>> PER_PERMISSIONS_TYPE = | ||
new TypeReference<Map<String, Collection<Permission>>>() {}; | ||
|
||
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 JsonNode perRolePermissionsNode = jsonNode.get("perRolePermissions"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: I understood that if both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
They cannot be. Let me add an assertion logic. 😉 |
||
|
||
final PerRolePermissions perRolePermissions; | ||
final Map<String, Collection<Permission>> perUserPermissions; | ||
final Map<String, Collection<Permission>> perTokenPermissions; | ||
if (perRolePermissionsNode != null) { | ||
// legacy format | ||
perRolePermissions = Jackson.treeToValue(perRolePermissionsNode, PerRolePermissions.class); | ||
perUserPermissions = Jackson.readValue(jsonNode.get("perUserPermissions").traverse(), | ||
PER_PERMISSIONS_TYPE); | ||
perTokenPermissions = Jackson.readValue(jsonNode.get("perTokenPermissions").traverse(), | ||
PER_PERMISSIONS_TYPE); | ||
} else { | ||
// new format | ||
final Roles roles = Jackson.treeToValue(jsonNode.get("roles"), Roles.class); | ||
perRolePermissions = new PerRolePermissions(READ_WRITE, getPermissions(roles.projectMember()), | ||
getPermissions(roles.projectGuest()), null); | ||
perUserPermissions = convert(roles.users()); | ||
perTokenPermissions = convert(roles.tokens()); | ||
} | ||
|
||
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>> convert( | ||
Map<String, RepositoryRole> roles) { | ||
return roles.entrySet().stream() | ||
.collect(toImmutableMap(Entry::getKey, entry -> getPermissions(entry.getValue()))); | ||
} | ||
|
||
static Collection<Permission> getPermissions(@Nullable RepositoryRole repositoryRole) { | ||
if (repositoryRole == null) { | ||
return ImmutableList.of(); | ||
} | ||
if (repositoryRole == RepositoryRole.READ) { | ||
return ImmutableList.of(Permission.READ); | ||
} | ||
// WRITE or ADMIN RepositoryRole. | ||
return ImmutableList.of(Permission.READ, Permission.WRITE); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* Roles for a repository. | ||
*/ | ||
public enum RepositoryRole { | ||
/** | ||
* Able to read a file from a repository. | ||
*/ | ||
READ, | ||
/** | ||
* Able to write a file to a repository. | ||
*/ | ||
WRITE, | ||
/** | ||
* Able to manage a repository. | ||
*/ | ||
ADMIN | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understood these custom deserializers will be removed once migration is complete
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is correct. 👍 👍