|
| 1 | +/* |
| 2 | + * Copyright 2018 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package com.netflix.metacat.common.server.properties; |
| 19 | + |
| 20 | +import com.netflix.metacat.common.QualifiedName; |
| 21 | +import com.netflix.metacat.common.exception.MetacatException; |
| 22 | +import com.netflix.servo.util.VisibleForTesting; |
| 23 | +import lombok.Data; |
| 24 | +import lombok.NonNull; |
| 25 | +import org.apache.commons.lang3.StringUtils; |
| 26 | + |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.HashSet; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +/** |
| 34 | + * Properties related to authorization. |
| 35 | + * |
| 36 | + * @author zhenl |
| 37 | + * @since 1.2.0 |
| 38 | + */ |
| 39 | +@Data |
| 40 | +public class AuthorizationServiceProperties { |
| 41 | + private boolean enabled; |
| 42 | + @NonNull |
| 43 | + private CreateAcl createAcl = new CreateAcl(); |
| 44 | + @NonNull |
| 45 | + private DeleteAcl deleteAcl = new DeleteAcl(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Create Acl properties. |
| 49 | + * |
| 50 | + * @author zhenl |
| 51 | + * @since 1.2.0 |
| 52 | + */ |
| 53 | + @Data |
| 54 | + public static class CreateAcl { |
| 55 | + private String createAclStr; |
| 56 | + private Map<QualifiedName, Set<String>> createAclMap; |
| 57 | + |
| 58 | + /** |
| 59 | + * Get the create acl map. |
| 60 | + * |
| 61 | + * @return The create acl map |
| 62 | + */ |
| 63 | + public Map<QualifiedName, Set<String>> getCreateAclMap() { |
| 64 | + if (createAclMap == null) { |
| 65 | + createAclMap = createAclStr == null ? new HashMap<>() |
| 66 | + : getAclMap(createAclStr); |
| 67 | + } |
| 68 | + return createAclMap; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Delete Acl properties. |
| 74 | + * |
| 75 | + * @author zhenl |
| 76 | + * @since 1.2.0 |
| 77 | + */ |
| 78 | + @Data |
| 79 | + public static class DeleteAcl { |
| 80 | + private String deleteAclStr; |
| 81 | + private Map<QualifiedName, Set<String>> deleteAclMap; |
| 82 | + |
| 83 | + /** |
| 84 | + * Get the delete acl map. |
| 85 | + * |
| 86 | + * @return The delete acl map |
| 87 | + */ |
| 88 | + public Map<QualifiedName, Set<String>> getDeleteAclMap() { |
| 89 | + if (deleteAclMap == null) { |
| 90 | + deleteAclMap = deleteAclStr == null ? new HashMap<>() |
| 91 | + : getAclMap(deleteAclStr); |
| 92 | + } |
| 93 | + return deleteAclMap; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Parse the configuration to get operation control. The control is at userName level |
| 99 | + * and the controlled operations include create, delete, and rename for table. |
| 100 | + * The format is below. |
| 101 | + * db1:user1,user2|db2:user1,user2 |
| 102 | + * |
| 103 | + * @param aclConfig the config strings for dbs |
| 104 | + * @return acl config |
| 105 | + */ |
| 106 | + @VisibleForTesting |
| 107 | + private static Map<QualifiedName, Set<String>> getAclMap(final String aclConfig) { |
| 108 | + final Map<QualifiedName, Set<String>> aclMap = new HashMap<>(); |
| 109 | + try { |
| 110 | + for (String aclstr : StringUtils.split(aclConfig, "|")) { |
| 111 | + for (QualifiedName key |
| 112 | + : PropertyUtils.delimitedStringsToQualifiedNamesList( |
| 113 | + aclstr.substring(0, aclstr.indexOf(":")), ',')) { |
| 114 | + aclMap.put(key, |
| 115 | + new HashSet<>(Arrays.asList( |
| 116 | + StringUtils.split(aclstr.substring(aclstr.indexOf(":") + 1), ",")))); |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + } catch (Exception e) { |
| 122 | + throw new MetacatException("metacat acl property parsing error" + e.getMessage()); |
| 123 | + } |
| 124 | + return aclMap; |
| 125 | + } |
| 126 | +} |
0 commit comments