Skip to content

Commit 192c561

Browse files
committed
add ecs ram role provider.
1 parent a1c3215 commit 192c561

File tree

4 files changed

+557
-0
lines changed

4 files changed

+557
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 com.aliyun.oss.common.utils.AuthUtils;
23+
import com.aliyun.oss.common.utils.LogUtils;
24+
import com.aliyuncs.exceptions.ClientException;
25+
26+
public class EcsRamRoleCredentialsProvider implements CredentialsProvider {
27+
28+
public EcsRamRoleCredentialsProvider(String ossAuthServerHost) {
29+
this.fetcher = new EcsRamRoleCredentialsFetcher(ossAuthServerHost);
30+
}
31+
32+
public EcsRamRoleCredentialsProvider withCredentialsFetcher(EcsRamRoleCredentialsFetcher fetcher) {
33+
this.fetcher = fetcher;
34+
return this;
35+
}
36+
37+
@Override
38+
public void setCredentials(Credentials creds) {
39+
40+
}
41+
42+
@Override
43+
public Credentials getCredentials() {
44+
if (credentials == null || credentials.willSoonExpire()) {
45+
try {
46+
credentials = (BasicCredentials) fetcher.fetch(maxRetryTimes);
47+
} catch (ClientException e) {
48+
LogUtils.logException("EcsRoleCredentialsProvider.fetch Exception:", e);
49+
return null;
50+
}
51+
}
52+
return credentials;
53+
}
54+
55+
private BasicCredentials credentials;
56+
private EcsRamRoleCredentialsFetcher fetcher;
57+
58+
private int maxRetryTimes = AuthUtils.MAX_ECS_METADATA_FETCH_RETRY_TIMES;
59+
60+
}

0 commit comments

Comments
 (0)