-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from rockuw/master
Add STS support
- Loading branch information
Showing
39 changed files
with
834 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,11 @@ | |
|
||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'aliyun/oss/version' | ||
require 'aliyun/version' | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = 'aliyun-sdk' | ||
spec.version = Aliyun::OSS::VERSION | ||
spec.version = Aliyun::VERSION | ||
spec.authors = ['Tianlong Wu'] | ||
spec.email = ['[email protected]'] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
$LOAD_PATH.unshift(File.expand_path("../../../../lib", __FILE__)) | ||
require 'yaml' | ||
require 'aliyun/sts' | ||
require 'aliyun/oss' | ||
|
||
# 初始化OSS client | ||
Aliyun::Common::Logging.set_log_level(Logger::DEBUG) | ||
conf_file = '~/.sts.yml' | ||
conf = YAML.load(File.read(File.expand_path(conf_file))) | ||
|
||
# 辅助打印函数 | ||
def demo(msg) | ||
puts "######### #{msg} ########" | ||
puts | ||
yield | ||
puts "-------------------------" | ||
puts | ||
end | ||
|
||
demo "Using STS" do | ||
sts = Aliyun::STS::Client.new( | ||
:access_key_id => conf['access_key_id'], | ||
:access_key_secret => conf['access_key_secret']) | ||
|
||
token = sts.assume_role( | ||
'acs:ram::52352:role/aliyunosstokengeneratorrole', 'app-1') | ||
|
||
client = Aliyun::OSS::Client.new( | ||
:endpoint => 'http://oss-cn-hangzhou.aliyuncs.com', | ||
:sts_token => token.security_token, | ||
:access_key_id => token.access_key_id, | ||
:access_key_secret => token.access_key_secret) | ||
|
||
unless client.bucket_exists?('bucket-for-sts-test') | ||
client.create_bucket('bucket-for-sts-test') | ||
end | ||
|
||
bucket = client.get_bucket('bucket-for-sts-test') | ||
|
||
bucket.put_object('hello') { |s| s << 'hello' } | ||
bucket.put_object('world') { |s| s << 'world' } | ||
|
||
bucket.list_objects.take(10).each do |obj| | ||
puts "Object: #{obj.key}, size: #{obj.size}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
$LOAD_PATH.unshift(File.expand_path("../../../../lib", __FILE__)) | ||
require 'yaml' | ||
require 'aliyun/sts' | ||
|
||
Aliyun::Common::Logging.set_log_level(Logger::DEBUG) | ||
conf_file = '~/.sts.yml' | ||
conf = YAML.load(File.read(File.expand_path(conf_file))) | ||
client = Aliyun::STS::Client.new( | ||
:access_key_id => conf['access_key_id'], | ||
:access_key_secret => conf['access_key_secret']) | ||
|
||
# 辅助打印函数 | ||
def demo(msg) | ||
puts "######### #{msg} ########" | ||
puts | ||
yield | ||
puts "-------------------------" | ||
puts | ||
end | ||
|
||
token = client.assume_role( | ||
'acs:ram::52352:role/aliyunosstokengeneratorrole', 'app-1') | ||
|
||
demo "Assume role" do | ||
begin | ||
token = client.assume_role( | ||
'acs:ram::52352:role/aliyunosstokengeneratorrole', 'app-1') | ||
|
||
puts "Credentials for session: #{token.session_name}" | ||
puts "access key id: #{token.access_key_id}" | ||
puts "access key secret: #{token.access_key_secret}" | ||
puts "security token: #{token.security_token}" | ||
puts "expiration at: #{token.expiration}" | ||
rescue => e | ||
puts "AssumeRole failed: #{e.message}" | ||
end | ||
end | ||
|
||
demo "Assume role with policy" do | ||
begin | ||
policy = Aliyun::STS::Policy.new | ||
policy.allow( | ||
['oss:Get*', 'oss:PutObject'], | ||
['acs:oss:*:*:my-bucket', 'acs:oss:*:*:my-bucket/*']) | ||
|
||
token = client.assume_role( | ||
'acs:ram::52352:role/aliyunosstokengeneratorrole', 'app-2', policy, 900) | ||
|
||
puts "Credentials for session: #{token.session_name}" | ||
puts "access key id: #{token.access_key_id}" | ||
puts "access key secret: #{token.access_key_secret}" | ||
puts "security token: #{token.security_token}" | ||
puts "expiration at: #{token.expiration}" | ||
rescue => e | ||
puts "AssumeRole failed: #{e.message}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
require_relative 'version' | ||
require_relative 'common/struct' | ||
require_relative 'common/logging' | ||
require_relative 'common/exception' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
module Aliyun | ||
module Common | ||
|
||
## | ||
# Base exception class | ||
# | ||
class Exception < RuntimeError | ||
attr_reader :message | ||
|
||
def initialize(message) | ||
@message = message | ||
end | ||
end | ||
|
||
end # Common | ||
end # Aliyun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
module Aliyun | ||
module Common | ||
|
||
# Common structs used. It provides a 'attrs' helper method for | ||
# subclass to define its attributes. 'attrs' is based on | ||
# attr_reader and provide additional functionalities for classes | ||
# that inherits Struct::Base : | ||
# * the constuctor is provided to accept options and set the | ||
# corresponding attibute automatically | ||
# * the #to_s method is rewrite to concatenate the defined | ||
# attributes keys and values | ||
# @example | ||
# class X < Struct::Base | ||
# attrs :foo, :bar | ||
# end | ||
# | ||
# x.new(:foo => 'hello', :bar => 'world') | ||
# x.foo # == "hello" | ||
# x.bar # == "world" | ||
# x.to_s # == "foo: hello, bar: world" | ||
module Struct | ||
class Base | ||
module AttrHelper | ||
def attrs(*s) | ||
define_method(:attrs) {s} | ||
attr_reader(*s) | ||
end | ||
end | ||
|
||
extend AttrHelper | ||
|
||
def initialize(opts = {}) | ||
extra_keys = opts.keys - attrs | ||
unless extra_keys.empty? | ||
fail Common::Exception, | ||
"Unexpected extra keys: #{extra_keys.join(', ')}" | ||
end | ||
|
||
attrs.each do |attr| | ||
instance_variable_set("@#{attr}", opts[attr]) | ||
end | ||
end | ||
|
||
def to_s | ||
attrs.map do |attr| | ||
v = instance_variable_get("@#{attr}") | ||
"#{attr.to_s}: #{v}" | ||
end.join(", ") | ||
end | ||
end # Base | ||
end # Struct | ||
|
||
end # Common | ||
end # Aliyun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.