|
| 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 com.aliyun.oss.common.auth; |
| 21 | + |
| 22 | +import java.net.MalformedURLException; |
| 23 | +import java.net.URL; |
| 24 | + |
| 25 | +import com.aliyun.oss.common.auth.Credentials; |
| 26 | +import com.aliyun.oss.common.utils.AuthUtils; |
| 27 | +import com.aliyuncs.exceptions.ClientException; |
| 28 | +import com.aliyuncs.http.HttpResponse; |
| 29 | +import org.codehaus.jettison.json.JSONException; |
| 30 | +import org.codehaus.jettison.json.JSONObject; |
| 31 | + |
| 32 | +public class EcsRamRoleCredentialsFetcher extends HttpCredentialsFetcher { |
| 33 | + |
| 34 | + public EcsRamRoleCredentialsFetcher(String ossAuthServerHost) { |
| 35 | + this.ossAuthServerHost = ossAuthServerHost; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public URL buildUrl() throws ClientException { |
| 40 | + try { |
| 41 | + return new URL(ossAuthServerHost); |
| 42 | + } catch (MalformedURLException e) { |
| 43 | + throw new IllegalArgumentException(e.toString()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public Credentials parse(HttpResponse response) throws ClientException { |
| 48 | + String jsonContent = new String(response.getHttpContent()); |
| 49 | + |
| 50 | + try { |
| 51 | + JSONObject jsonObject = new JSONObject(jsonContent); |
| 52 | + |
| 53 | + if (!jsonObject.has("Code")) { |
| 54 | + throw new ClientException("Invalid json " + jsonContent + " got from ecs metadata server."); |
| 55 | + } |
| 56 | + |
| 57 | + if (!"Success".equals(jsonObject.get("Code"))) { |
| 58 | + throw new ClientException("Failed to get credentials from ecs metadata server"); |
| 59 | + } |
| 60 | + |
| 61 | + if (!jsonObject.has("AccessKeyId") || !jsonObject.has("AccessKeySecret")) { |
| 62 | + throw new ClientException("Invalid json " + jsonContent + " got from ecs metadata server."); |
| 63 | + } |
| 64 | + |
| 65 | + String securityToken = null; |
| 66 | + if (jsonObject.has("SecurityToken")) { |
| 67 | + securityToken = jsonObject.getString("SecurityToken"); |
| 68 | + } |
| 69 | + |
| 70 | + if (jsonObject.has("Expiration")) { |
| 71 | + return new InstanceProfileCredentials(jsonObject.getString("AccessKeyId"), |
| 72 | + jsonObject.getString("AccessKeySecret"), securityToken, jsonObject.getString("Expiration")) |
| 73 | + .withExpiredDuration( |
| 74 | + AuthUtils.DEFAULT_STS_SESSION_TOKEN_DURATION_SECONDS); |
| 75 | + } |
| 76 | + |
| 77 | + return new BasicCredentials(jsonObject.getString("AccessKeyId"), jsonObject.getString("AccessKeySecret"), |
| 78 | + securityToken); |
| 79 | + } catch (JSONException e) { |
| 80 | + throw new ClientException("EcsRamRoleCredentialsFetcher.parse [" + jsonContent + "] exception:" + e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private String ossAuthServerHost; |
| 85 | +} |
0 commit comments