16
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS STSで期限付きのサーバ認証トークン発行

Last updated at Posted at 2013-10-21

概要

以下について調べたことをまとめる

  • 期限付き証明書の取得方法
  • 期限切れの確認方法
  • トークンの管理方法

環境

以下を使用して調査

  • ruby-1.9.3-p385
  • aws-sdk

結果

期限付き証明書の取得方法

その他

セッショントークンの期限チェック

  • できなさそう
  • ruby-aws-sdkには、セッショントークン取得メソッドしかない
  • new_session
  • new_federated_session

参考

検証コード

S3アップロード権限を付けた一時的証明書を発行して、アップロードしてみる

require 'aws-sdk'

user_name   = "test_user"
bucket_name = "test_bucket"
object_nmae = "test_object"
file_name   = "hoge.txt"

## 一時的証明書取得
opt = { access_key_id: "**********",
        secret_access_key: "**********" }
upload_policy = AWS::STS::Policy.new do |policy|
  policy.allow( actions: ['s3:PutObject'], resources: "arn:aws:s3:::#{bucket_name}/#{object_name}/*")
end
duration = 60*60

upload_session = AWS::STS.new(opt).new_federated_session("user_name", policy: upload_policy, duration: duration)

res = { access_key_id:     upload_session.credentials[:access_key_id],
        secret_access_key: upload_session.credentials[:secret_access_key],
        session_token:     upload_session.credentials[:session_token],
        expires_date:      upload_session.expires_at.to_i.to_s }

## S3アップロード
AWS.config(
  :access_key_id     => res[:access_key_id],
  :secret_access_key => res[:secret_access_key],
  :session_token     => res[:session_token],
  :s3_endpoint   => 's3-ap-northeast-1.amazonaws.com',
)
s3 = AWS::S3.new
bucket = s3.buckets[bucket_name]
unless bucket.exists?
  puts "S3 Bucket No Exist"
  exit 1
end

object = bucket.objects[object_name].write(:file => file_name)
16
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?