一. 權限介紹
1. 為什麼需要授權
RAM授權
您可以通過RAM建立、管理使用者賬号(例如員工、系統或應用程式),并控制這些使用者賬号對您名下資源具有的操作權限。當您的企業存在多使用者協同操作資源時,使用RAM可以讓您避免與其他使用者共享雲賬号密鑰,按需為使用者配置設定最小權限,進而降低您的企業資訊安全風險。
為了更精細地管理和記錄檔服務資源,您可以通過阿裡雲RAM産品為您名下的子賬号、日志服務的RAM服務角色和使用者角色賦予相應的通路權限。
STS授權
阿裡雲賬号可以通過建立并授權使用者角色的方式賦予其他雲賬号一定的資源權限,其他雲賬号扮演該角色,并為其名下的RAM使用者授予AssumeRole權限之後,其他雲賬号或其子賬号可以通過通路STS接口擷取臨時AK/Token,調用日志服務API接口。
通過STS授權,您可以使用臨時的AK操作,而不用擔心AK的洩漏問題,更加安全。
2. 有哪些的授權方式
概覽
https://help.aliyun.com/document_detail/29049.htmlhttps://help.aliyun.com/document_detail/47664.html
RAM自定義授權場景
https://help.aliyun.com/document_detail/89676.html通過STS授權
https://help.aliyun.com/document_detail/47277.html二. 問題排查
1. 配置儲存不成功
1.1 檢查是否為json格式
将配置粘貼到
https://www.json.cn/中,檢查是否有格式錯誤。
1.2 檢查是否和樣例的樣式比對
樣例參考:
檢查是否有多個"Action"或多個"Resource"在同一級别下。
2. 配置儲存成功提示沒有權限
2.1 檢查是否配置了對應的權限
通常沒有權限報錯會提示出需要配置的具體權限,參考提示把相應的action和resource加入到授權的政策中。
{"errorCode": "Unauthorized", "errorMessage": "denied by sts or ram, action: log:GetLogStoreLogs, resource: acs:log:cn-beijing:1641920:project/project_name/logstore/logstore_name", "requestId": "5E01B161A1F9CBE3**"}
如上報錯,沒有log:GetLogStoreLogs權限,操作如下:
1.找到沒有權限的RAM使用者(
https://ram.console.aliyun.com/users)

2.點選進入使用者的權限管理界面
3.點選檢視使用者的權限政策裡是否有對應的權限
action: log:GetLogStoreLogs
resource: acs:log:cn-beijing:1641920*:project/project_name/logstore/logstore_name
4.點選修改政策内容,在Statement裡添加相應的權限
其中cn-beijing(region)和1641920(aliuid)可以用通配符代替。
點選确定配置生效。
2.2 常見無權限場景
2.2.1 無權限設定告警
主要注意第三個,Resource job:
https://help.aliyun.com/document_detail/109663.html2.2.2 無權限檢視服務日志(project概覽頁提示權限問題)
參考文檔最下的授權配置,主要是沒有概覽頁檢視服務日志的權限:
https://help.aliyun.com/document_detail/85536.html2.2.3 無權限投遞oss(RAM角色配置問題)
參考文檔,RAM角色沒有設定對:
https://help.aliyun.com/document_detail/29002.html參考文檔中動圖:
2.3 sts權限問題
2.3.1 簡要介紹
注意:子賬号要有 "Action": "sts:AssumeRole" 權限;角色有日志服務的權限。
sts服務代碼:
AssumeRoleRequest request = new AssumeRoleRequest();
request.setRoleSessionName("XXX");
request.setRoleArn("XXX");
AssumeRoleResponse response = client.getAcsResponse(request);
其中RoleSessionName為您設定的授權名字(自己定義的)
RoleArn為角色的名字(需要有日志服務的權限)
調用的子賬号要有sts:AssumeRole權限
2.3.2 詳細說明(擷取臨時AK和token)
1.建立角色
2.為角色添加日志服務的權限
3.擷取角色的Arn
acs:ram::104**12:role/sts-test
4.建立RAM使用者(
5.為RAM使用者添加AliyunSTSAssumeRoleAccess權限
6.通過接口擷取臨時AK/Token
Java代碼樣例
@Controller
public class DemoController {
@ResponseBody
@RequestMapping("get")
public String get(){
String region = "cn-hangzhou";
DefaultProfile profile = DefaultProfile._getProfile_(region, "ak_id", "ak_secret");
IAcsClient client = new DefaultAcsClient(profile);
AssumeRoleRequest request = new AssumeRoleRequest();
request.setRoleSessionName("sts-test");
request.setRoleArn("acs:ram::10412:role/sts-test"**);
try {
AssumeRoleResponse response = client.getAcsResponse(request);
AssumeRoleResponse.Credentials sts = response.getCredentials();
return sts.getAccessKeyId()+","+sts.getAccessKeySecret()+","+sts.getSecurityToken();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
2.4 緩存問題
如果配置正确提示沒有權限,很有可能是緩存的問題。
日志服務有很多前端機器,前端機器會緩存使用者的權限資訊,修改配置後,一部分機器的緩存還沒有重新整理。
發送請求到有緩存的前端機器上,會提示權限問題,之後機器的權限緩存會重新整理一下,這個問題之後會優化解決。
權限緩存重新整理工具
重新整理1000次:
./sls-refresh-permissions-tool -id accessKeyID -secret accessKeySecret -e endpoint -p project -l logstore -t 1000
相關參數
https://help.aliyun.com/document_detail/29064.html1.mac版本
https://test-lichao.oss-cn-hangzhou.aliyuncs.com/permissions-tool/darwin/sls-refresh-permissions-tool2.linux版本
https://test-lichao.oss-cn-hangzhou.aliyuncs.com/permissions-tool/linux/sls-refresh-permissions-tool3.windows版本
https://test-lichao.oss-cn-hangzhou.aliyuncs.com/permissions-tool/windows/sls-refresh-permissions-tool.exe