|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.gravitino.credential; |
| 21 | + |
| 22 | +import com.google.common.base.Preconditions; |
| 23 | +import com.google.common.collect.ImmutableMap; |
| 24 | +import java.util.Map; |
| 25 | +import org.apache.commons.lang3.StringUtils; |
| 26 | + |
| 27 | +/** Azure account key credential. */ |
| 28 | +public class AzureAccountKeyCredential implements Credential { |
| 29 | + |
| 30 | + /** Azure account key credential type. */ |
| 31 | + public static final String AZURE_ACCOUNT_KEY_CREDENTIAL_TYPE = "azure-account-key"; |
| 32 | + /** Azure storage account name */ |
| 33 | + public static final String GRAVITINO_AZURE_STORAGE_ACCOUNT_NAME = "azure-storage-account-name"; |
| 34 | + /** Azure storage account key */ |
| 35 | + public static final String GRAVITINO_AZURE_STORAGE_ACCOUNT_KEY = "azure-storage-account-key"; |
| 36 | + |
| 37 | + private String accountName; |
| 38 | + private String accountKey; |
| 39 | + |
| 40 | + /** |
| 41 | + * Constructs an instance of {@link AzureAccountKeyCredential}. |
| 42 | + * |
| 43 | + * @param accountName The Azure account name. |
| 44 | + * @param accountKey The Azure account key. |
| 45 | + */ |
| 46 | + public AzureAccountKeyCredential(String accountName, String accountKey) { |
| 47 | + validate(accountName, accountKey); |
| 48 | + this.accountName = accountName; |
| 49 | + this.accountKey = accountKey; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * This is the constructor that is used by credential factory to create an instance of credential |
| 54 | + * according to the credential information. |
| 55 | + */ |
| 56 | + public AzureAccountKeyCredential() {} |
| 57 | + |
| 58 | + @Override |
| 59 | + public String credentialType() { |
| 60 | + return AZURE_ACCOUNT_KEY_CREDENTIAL_TYPE; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public long expireTimeInMs() { |
| 65 | + return 0; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Map<String, String> credentialInfo() { |
| 70 | + return (new ImmutableMap.Builder<String, String>()) |
| 71 | + .put(GRAVITINO_AZURE_STORAGE_ACCOUNT_NAME, accountName) |
| 72 | + .put(GRAVITINO_AZURE_STORAGE_ACCOUNT_KEY, accountKey) |
| 73 | + .build(); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void initialize(Map<String, String> credentialInfo, long expireTimeInMS) { |
| 78 | + String accountName = credentialInfo.get(GRAVITINO_AZURE_STORAGE_ACCOUNT_NAME); |
| 79 | + String accountKey = credentialInfo.get(GRAVITINO_AZURE_STORAGE_ACCOUNT_KEY); |
| 80 | + validate(accountName, accountKey); |
| 81 | + this.accountName = accountName; |
| 82 | + this.accountKey = accountKey; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get Azure account name |
| 87 | + * |
| 88 | + * @return The Azure account name |
| 89 | + */ |
| 90 | + public String accountName() { |
| 91 | + return accountName; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get Azure account key |
| 96 | + * |
| 97 | + * @return The Azure account key |
| 98 | + */ |
| 99 | + public String accountKey() { |
| 100 | + return accountKey; |
| 101 | + } |
| 102 | + |
| 103 | + private void validate(String accountName, String accountKey) { |
| 104 | + Preconditions.checkArgument( |
| 105 | + StringUtils.isNotBlank(accountName), "Azure account name should not be empty."); |
| 106 | + Preconditions.checkArgument( |
| 107 | + StringUtils.isNotBlank(accountKey), "Azure account key should not be empty."); |
| 108 | + } |
| 109 | +} |
0 commit comments