Skip to content

Commit 2f654db

Browse files
authored
Merge pull request #16 from john-shaffer/15-allow-use-of-iam-roles
Allow use of IAM roles
2 parents fddc6d8 + b8f1e0c commit 2f654db

1 file changed

Lines changed: 70 additions & 47 deletions

File tree

src/Deployer.php

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,8 @@ public function upload_files( string $processed_site_path ) : void {
2424
return;
2525
}
2626

27-
$client_options = [
28-
'profile' => Controller::getValue( 's3Profile' ),
29-
'version' => 'latest',
30-
'region' => Controller::getValue( 's3Region' ),
31-
];
32-
33-
/*
34-
If no credentials option, SDK attempts to load credentials from
35-
your environment in the following order:
36-
37-
- environment variables.
38-
- a credentials .ini file.
39-
- an IAM role.
40-
*/
41-
if (
42-
Controller::getValue( 's3AccessKeyID' ) &&
43-
Controller::getValue( 's3SecretAccessKey' )
44-
) {
45-
$client_options['credentials'] = [
46-
'key' => Controller::getValue( 's3AccessKeyID' ),
47-
'secret' => \WP2Static\CoreOptions::encrypt_decrypt(
48-
'decrypt',
49-
Controller::getValue( 's3SecretAccessKey' )
50-
),
51-
];
52-
unset( $client_options['profile'] );
53-
}
54-
5527
// instantiate S3 client
56-
$s3 = new \Aws\S3\S3Client( $client_options );
28+
$s3 = self::s3_client();
5729

5830
// iterate each file in ProcessedSite
5931
$iterator = new RecursiveIteratorIterator(
@@ -114,44 +86,95 @@ public function upload_files( string $processed_site_path ) : void {
11486
}
11587
}
11688

89+
public function s3_client() : \Aws\S3\S3Client {
90+
$client_options = [
91+
'version' => 'latest',
92+
'region' => Controller::getValue( 's3Region' ),
93+
];
94+
95+
/*
96+
If no credentials option, SDK attempts to load credentials from
97+
your environment in the following order:
11798
118-
public function cloudfront_invalidate_all_items() : void {
119-
if ( ! Controller::getValue( 'cfDistributionID' ) ) {
120-
return;
99+
- environment variables.
100+
- a credentials .ini file.
101+
- an IAM role.
102+
*/
103+
if (
104+
Controller::getValue( 's3AccessKeyID' ) &&
105+
Controller::getValue( 's3SecretAccessKey' )
106+
) {
107+
$client_options['credentials'] = [
108+
'key' => Controller::getValue( 's3AccessKeyID' ),
109+
'secret' => \WP2Static\CoreOptions::encrypt_decrypt(
110+
'decrypt',
111+
Controller::getValue( 's3SecretAccessKey' )
112+
),
113+
];
114+
} else if ( Controller::getValue( 's3Profile' ) ) {
115+
$client_options['profile'] = Controller::getValue( 's3Profile' );
121116
}
122117

123-
\WP2Static\WsLog::l( 'Invalidating all CloudFront items' );
118+
return new \Aws\S3\S3Client( $client_options );
119+
}
124120

121+
public function cloudfront_client() : \Aws\CloudFront\CloudFrontClient {
125122
/*
126123
If no credentials option, SDK attempts to load credentials from
127124
your environment in the following order:
128-
129125
- environment variables.
130126
- a credentials .ini file.
131127
- an IAM role.
132128
*/
133129
if (
134-
Controller::getValue( 's3AccessKeyID' ) &&
135-
Controller::getValue( 's3SecretAccessKey' )
130+
Controller::getValue( 'cfAccessKeyID' ) &&
131+
Controller::getValue( 'cfSecretAccessKey' )
136132
) {
137-
133+
// Use the supplied access keys.
138134
$credentials = new \Aws\Credentials\Credentials(
139-
Controller::getValue( 's3AccessKeyID' ),
135+
Controller::getValue( 'cfAccessKeyID' ),
140136
\WP2Static\CoreOptions::encrypt_decrypt(
141137
'decrypt',
142-
Controller::getValue( 's3SecretAccessKey' )
138+
Controller::getValue( 'cfSecretAccessKey' )
143139
)
144140
);
141+
$client = \Aws\CloudFront\CloudFrontClient::factory(
142+
[
143+
'region' => Controller::getValue( 'cfRegion' ),
144+
'version' => 'latest',
145+
'credentials' => $credentials,
146+
]
147+
);
148+
} else if ( Controller::getValue( 'cfProfile' ) ) {
149+
// Use the specified profile.
150+
$client = \Aws\CloudFront\CloudFrontClient::factory(
151+
[
152+
'profile' => Controller::getValue( 'cfProfile' ),
153+
'region' => Controller::getValue( 'cfRegion' ),
154+
'version' => 'latest',
155+
]
156+
);
157+
} else {
158+
// Use the IAM role.
159+
$client = \Aws\CloudFront\CloudFrontClient::factory(
160+
[
161+
'region' => Controller::getValue( 'cfRegion' ),
162+
'version' => 'latest',
163+
]
164+
);
145165
}
146166

147-
$client = \Aws\CloudFront\CloudFrontClient::factory(
148-
[
149-
'profile' => Controller::getValue( 'cfProfile' ),
150-
'region' => Controller::getValue( 'cfRegion' ),
151-
'version' => 'latest',
152-
'credentials' => isset( $credentials ) ? $credentials : '',
153-
]
154-
);
167+
return $client;
168+
}
169+
170+
public function cloudfront_invalidate_all_items() : void {
171+
if ( ! Controller::getValue( 'cfDistributionID' ) ) {
172+
return;
173+
}
174+
175+
\WP2Static\WsLog::l( 'Invalidating all CloudFront items' );
176+
177+
$client = self::cloudfront_client();
155178

156179
try {
157180
$result = $client->createInvalidation(

0 commit comments

Comments
 (0)