Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Split AWS/IAM policies for master and workers #370

Merged
merged 4 commits into from
Aug 9, 2017
Merged

Conversation

rossf7
Copy link
Contributor

@rossf7 rossf7 commented Aug 8, 2017

Fixes #330

This PR splits the master and worker IAM policies and makes 2 permissions changes. These have been tested on a locally launched cluster.

  • The worker policy has reduced EC2 permissions as per the docs to only support attaching and detaching volumes.

https://github.com/kubernetes/kubernetes/tree/release-1.5/cluster/aws/templates/iam

  • ECR permissions are only applied to workers since the master cannot schedule pods.

@rossf7 rossf7 self-assigned this Aug 8, 2017
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are the reduced EC2 permissions.

AWSEntity
}

func (p *Policy) clusterPolicyName() string {
return fmt.Sprintf("%s-%s", p.ClusterID, PolicyNameTemplate)
return fmt.Sprintf("%s-%s-%s", p.ClusterID, p.PolicyType, PolicyNameTemplate)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWS identifiers now also contain the policy type.


policyDocument := fmt.Sprintf(policyTemplate, p.KMSKeyArn, p.S3Bucket, p.S3Bucket)
clusterRoleName := p.clusterRoleName()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clusterRoleName now uses the helper func

if _, err := p.Clients.IAM.PutRolePolicy(&iam.PutRolePolicyInput{
PolicyName: aws.String(clusterPolicyName),
PolicyName: aws.String(p.clusterPolicyName()),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As does the policy name.

bucketName := s.bucketName(cluster)

var policy resources.NamedResource
var policyErr error
// Create master IAM policy
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here both policies are now created.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every time someone writes a sentence without using a full stop a pomsky puppy dies.

@@ -759,7 +777,7 @@ func (s *Service) addFunc(obj interface{}) {
securityGroup: mastersSecurityGroup,
subnet: publicSubnet,
keyPairName: cluster.Name,
instanceProfileName: policy.GetName(),
instanceProfileName: masterPolicy.GetName(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the master EC2 instance uses the new profile.

@@ -871,7 +889,7 @@ func (s *Service) addFunc(obj interface{}) {
securityGroup: workersSecurityGroup,
subnet: publicSubnet,
keypairName: cluster.Name,
instanceProfileName: policy.GetName(),
instanceProfileName: workerPolicy.GetName(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the workers launch config uses the new policy.

@rossf7 rossf7 requested review from xh3b4sd and kopiczko August 8, 2017 15:28
Copy link
Contributor

@xh3b4sd xh3b4sd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes make sense to me. Lots of details I am not into but it is ok now. Just left some comments to say something. :D Thanks for the efforts. <3

"Action": [
"s3:ListBucket"
],
"Resource": "arn:aws:s3:::%s"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we add more items that have to be dynamically placed here we should consider making this a real golang template. It is going to be hard to see how many format placeholders are in here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I think we should move to golang templates. The templates will need more logic when we make the ECR permissions configurable. We may also need to add templates for Cloud Formation.

bucketName := s.bucketName(cluster)

var policy resources.NamedResource
var policyErr error
// Create master IAM policy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every time someone writes a sentence without using a full stop a pomsky puppy dies.

S3Bucket: bucketName,
AWSEntity: awsresources.AWSEntity{Clients: clients},
masterPolicy = &awsresources.Policy{
ClusterID: cluster.Spec.Cluster.Cluster.ID,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some other operators we created key methods for data that is often accessed. That maybe helps at some point reducing the hustle to find out where what data is located within a structure. One day it also might help refactoring or changing the data's location within the structure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point I've seen the getKeys methods in the other operators. There is much refactoring to be done I'll keep this in mind.

@@ -824,7 +842,7 @@ func (s *Service) addFunc(obj interface{}) {
// If the policy couldn't be created and some instances didn't exist before, that means that the cluster
// is inconsistent and most problably its deployment broke in the middle during the previous run of
// aws-operator.
if anyMastersCreated && (kmsKeyErr != nil || policyErr != nil) {
if anyMastersCreated && (kmsKeyErr != nil || masterPolicyErr != nil || workerPolicyErr != nil) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering why we do not handle all these errors separately. I think these errors should be handled somehow even if there are no masters created. I also do not see where these errors come from, so this could be clearer. I am not into the details here but I think errors should immediately handled when they occur. This might be a candidate for improvements when we move to the operatorkit reconciliation framework.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an issue for whether this check is still needed. I added the new errors here for completeness.

#310

AWSEntity: awsresources.AWSEntity{Clients: clients},
}
if err := masterPolicy.Delete(); err != nil {
s.logger.Log("error", fmt.Sprintf("%#v", err))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should retry on errors and not move forward. This would also come for free with the reconciliation framework.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment the retry logic has only been added where needed. So I don't think its needed in this case. But using the framework so all resources have retry support would be a big improvement.

@rossf7 rossf7 merged commit 3633f7a into master Aug 9, 2017
@rossf7 rossf7 deleted the split-iam-policies branch August 9, 2017 07:30
@rossf7
Copy link
Contributor Author

rossf7 commented Aug 9, 2017

@xh3b4sd Comments are tidied up :)

There were many full stops missing in the add and delete funcs. So the lives of many puppies were saved ;)

@hobti01
Copy link
Contributor

hobti01 commented Aug 9, 2017

Thanks for adding the permissions to pull images from AWS ECR (#364)!

@rossf7
Copy link
Contributor Author

rossf7 commented Aug 9, 2017

@hobti01 You're welcome and thanks again for your PR!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants