-
-
Notifications
You must be signed in to change notification settings - Fork 352
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 #60 from fcheung/iam_attach_policy
Support for IAM managed policies
- Loading branch information
Showing
16 changed files
with
529 additions
and
0 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
Binary file not shown.
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,29 @@ | ||
module Fog | ||
module Parsers | ||
module AWS | ||
module IAM | ||
require 'fog/aws/parsers/iam/policy_parser' | ||
class ListManagedPolicies < Fog::Parsers::AWS::IAM::PolicyParser | ||
def reset | ||
super | ||
@response = { 'Policies' => [] , 'Marker' => '', 'IsTruncated' => false} | ||
end | ||
|
||
def finished_policy(policy) | ||
@response['Policies'] << policy | ||
end | ||
|
||
def end_element(name) | ||
case name | ||
when 'RequestId', 'Marker' | ||
@response[name] = value | ||
when 'IsTruncated' | ||
@response[name] = (value == 'true') | ||
end | ||
super | ||
end | ||
end | ||
end | ||
end | ||
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,57 @@ | ||
module Fog | ||
module Parsers | ||
module AWS | ||
module IAM | ||
class PolicyParser < Fog::Parsers::Base | ||
def reset | ||
@policy = fresh_policy | ||
@stack = [] | ||
end | ||
|
||
def start_element(name,attrs = []) | ||
case name | ||
when 'Policies' | ||
@stack << name | ||
when 'Policy' | ||
@role =fresh_policy | ||
when 'member' | ||
if @stack.last == 'Policies' | ||
@role = fresh_policy | ||
end | ||
end | ||
super | ||
end | ||
|
||
def fresh_policy | ||
{'AttachmentCount' => 0, 'Description' => ''} | ||
end | ||
|
||
def end_element(name) | ||
case name | ||
when 'Arn', 'DefaultVersionId', 'Description', 'Path', 'PolicyName', 'PolicyId' | ||
@policy[name] = value | ||
when 'CreateDate', 'UpdateDate' | ||
@policy[name] = Time.parse(value) | ||
when 'IsAttachable' | ||
@policy[name] = (value == 'true') | ||
when 'AttachmentCount' | ||
@policy[name] = value.to_i | ||
when 'Policy' | ||
finished_policy(@policy) | ||
@policy = nil | ||
when 'Policies' | ||
if @stack.last == 'Policies' | ||
@stack.pop | ||
end | ||
when 'member' | ||
if @stack.last == 'Policies' | ||
finished_policy(@policy) | ||
@policy = nil | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
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,27 @@ | ||
module Fog | ||
module Parsers | ||
module AWS | ||
module IAM | ||
require 'fog/aws/parsers/iam/policy_parser' | ||
class SinglePolicy < Fog::Parsers::AWS::IAM::PolicyParser | ||
def reset | ||
super | ||
@response = { 'Policy' => {} } | ||
end | ||
|
||
def finished_policy(policy) | ||
@response['Policy'] = policy | ||
end | ||
|
||
def end_element(name) | ||
case name | ||
when 'RequestId' | ||
@response[name] = value | ||
end | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Binary file not shown.
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,32 @@ | ||
module Fog | ||
module AWS | ||
class IAM | ||
class Real | ||
require 'fog/aws/parsers/iam/basic' | ||
|
||
# Attaches a managed policy to a group | ||
# | ||
# ==== Parameters | ||
# * group_name<~String>: name of the group | ||
# * policy_arn<~String>: arn of the managed policy | ||
# | ||
# ==== Returns | ||
# * response<~Excon::Response>: | ||
# * body<~Hash>: | ||
# * 'RequestId'<~String> - Id of the request | ||
# | ||
# ==== See Also | ||
# http://docs.aws.amazon.com/IAM/latest/APIReference/API_AttachGroupPolicy.html | ||
# | ||
def attach_group_policy(group_name, policy_arn) | ||
request( | ||
'Action' => 'AttachGroupPolicy', | ||
'GroupName' => group_name, | ||
'PolicyArn' => policy_arn, | ||
:parser => Fog::Parsers::AWS::IAM::Basic.new | ||
) | ||
end | ||
end | ||
end | ||
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,32 @@ | ||
module Fog | ||
module AWS | ||
class IAM | ||
class Real | ||
require 'fog/aws/parsers/iam/basic' | ||
|
||
# Attaches a managed policy to a role | ||
# | ||
# ==== Parameters | ||
# * role_name<~String>: name of the role | ||
# * policy_arn<~String>: arn of the managed policy | ||
# | ||
# ==== Returns | ||
# * response<~Excon::Response>: | ||
# * body<~Hash>: | ||
# * 'RequestId'<~String> - Id of the request | ||
# | ||
# ==== See Also | ||
# http://docs.aws.amazon.com/IAM/latest/APIReference/API_AttachRolePolicy.html | ||
# | ||
def attach_role_policy(role_name, policy_arn) | ||
request( | ||
'Action' => 'AttachRolePolicy', | ||
'RoleName' => role_name, | ||
'PolicyArn' => policy_arn, | ||
:parser => Fog::Parsers::AWS::IAM::Basic.new | ||
) | ||
end | ||
end | ||
end | ||
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,32 @@ | ||
module Fog | ||
module AWS | ||
class IAM | ||
class Real | ||
require 'fog/aws/parsers/iam/basic' | ||
|
||
# Attaches a managed policy to a user | ||
# | ||
# ==== Parameters | ||
# * user_name<~String>: name of the user | ||
# * policy_arn<~String>: arn of the managed policy | ||
# | ||
# ==== Returns | ||
# * response<~Excon::Response>: | ||
# * body<~Hash>: | ||
# * 'RequestId'<~String> - Id of the request | ||
# | ||
# ==== See Also | ||
# http://docs.aws.amazon.com/IAM/latest/APIReference/API_AttachUserPolicy.html | ||
# | ||
def attach_user_policy(user_name, policy_arn) | ||
request( | ||
'Action' => 'AttachUserPolicy', | ||
'UserName' => user_name, | ||
'PolicyArn' => policy_arn, | ||
:parser => Fog::Parsers::AWS::IAM::Basic.new | ||
) | ||
end | ||
end | ||
end | ||
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,47 @@ | ||
module Fog | ||
module AWS | ||
class IAM | ||
class Real | ||
require 'fog/aws/parsers/iam/single_policy' | ||
|
||
# Creates a managed policy | ||
# | ||
# ==== Parameters | ||
# * policy_name<~String>: name of policy document | ||
# * policy_document<~Hash>: policy document, see: http://docs.amazonwebservices.com/IAM/latest/UserGuide/PoliciesOverview.html | ||
# * path <~String>: path of the policy | ||
# * description <~String>: description for the policy | ||
# ==== Returns | ||
# * response<~Excon::Response>: | ||
# * body<~Hash>: | ||
# * 'RequestId'<~String> - Id of the request | ||
# * 'Policy'<~Hash>: | ||
# * Arn | ||
# * AttachmentCount | ||
# * CreateDate | ||
# * DefaultVersionId | ||
# * Description | ||
# * IsAttachable | ||
# * Path | ||
# * PolicyId | ||
# * PolicyName | ||
# * UpdateDate | ||
# ==== See Also | ||
# http://docs.aws.amazon.com/IAM/latest/APIReference/API_CreatePolicy.html | ||
# | ||
def create_policy(policy_name, policy_document, path=nil, description=nil) | ||
request({ | ||
'Action' => 'CreatePolicy', | ||
'PolicyName' => policy_name, | ||
'PolicyDocument' => Fog::JSON.encode(policy_document), | ||
'Path' => path, | ||
'Description' => description, | ||
:parser => Fog::Parsers::AWS::IAM::SinglePolicy.new | ||
}.reject {|_, value| value.nil?}) | ||
end | ||
end | ||
|
||
|
||
end | ||
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,30 @@ | ||
module Fog | ||
module AWS | ||
class IAM | ||
class Real | ||
require 'fog/aws/parsers/iam/basic' | ||
|
||
# Deletes a manged policy | ||
# | ||
# ==== Parameters | ||
# * policy_arn<~String>: arn of the policy | ||
# | ||
# ==== Returns | ||
# * response<~Excon::Response>: | ||
# * body<~Hash>: | ||
# * 'RequestId'<~String> - Id of the request | ||
# | ||
# ==== See Also | ||
# http://docs.aws.amazon.com/IAM/latest/APIReference/API_DeletePolicy.html | ||
# | ||
def delete_policy(policy_arn) | ||
request( | ||
'Action' => 'DeletePolicy', | ||
'PolicyArn' => policy_arn, | ||
:parser => Fog::Parsers::AWS::IAM::Basic.new | ||
) | ||
end | ||
end | ||
end | ||
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,32 @@ | ||
module Fog | ||
module AWS | ||
class IAM | ||
class Real | ||
require 'fog/aws/parsers/iam/basic' | ||
|
||
# Detaches a managed policy from a group | ||
# | ||
# ==== Parameters | ||
# * group_name<~String>: name of the group | ||
# * policy_arn<~String>: arn of the managed policy | ||
# | ||
# ==== Returns | ||
# * response<~Excon::Response>: | ||
# * body<~Hash>: | ||
# * 'RequestId'<~String> - Id of the request | ||
# | ||
# ==== See Also | ||
# http://docs.aws.amazon.com/IAM/latest/APIReference/API_DetachGroupPolicy.html | ||
# | ||
def detach_group_policy(group_name, policy_arn) | ||
request( | ||
'Action' => 'DetachGroupPolicy', | ||
'GroupName' => group_name, | ||
'PolicyArn' => policy_arn, | ||
:parser => Fog::Parsers::AWS::IAM::Basic.new | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.