Skip to content

Commit 11f2aae

Browse files
committed
Improve CF client creation and move it to Deployer::cloudfront_client.
The improvements allow for the use of IAM roles rather than specifying access keys.
1 parent f218c96 commit 11f2aae

1 file changed

Lines changed: 37 additions & 18 deletions

File tree

src/Deployer.php

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,10 @@ public function upload_files( string $processed_site_path ) : void {
114114
}
115115
}
116116

117-
118-
public function cloudfront_invalidate_all_items() : void {
119-
if ( ! Controller::getValue( 'cfDistributionID' ) ) {
120-
return;
121-
}
122-
123-
\WP2Static\WsLog::l( 'Invalidating all CloudFront items' );
124-
117+
public function cloudfront_client() : \Aws\CloudFront\CloudFrontClient {
125118
/*
126119
If no credentials option, SDK attempts to load credentials from
127120
your environment in the following order:
128-
129121
- environment variables.
130122
- a credentials .ini file.
131123
- an IAM role.
@@ -134,24 +126,51 @@ public function cloudfront_invalidate_all_items() : void {
134126
Controller::getValue( 'cfAccessKeyID' ) &&
135127
Controller::getValue( 'cfSecretAccessKey' )
136128
) {
137-
129+
// Use the supplied access keys.
138130
$credentials = new \Aws\Credentials\Credentials(
139131
Controller::getValue( 'cfAccessKeyID' ),
140132
\WP2Static\CoreOptions::encrypt_decrypt(
141133
'decrypt',
142134
Controller::getValue( 'cfSecretAccessKey' )
143135
)
144136
);
137+
$client = \Aws\CloudFront\CloudFrontClient::factory(
138+
[
139+
'region' => Controller::getValue( 'cfRegion' ),
140+
'version' => 'latest',
141+
'credentials' => $credentials,
142+
]
143+
);
144+
} else if ( Controller::getValue( 'cfProfile' ) ) {
145+
// Use the specified profile.
146+
$client = \Aws\CloudFront\CloudFrontClient::factory(
147+
[
148+
'profile' => Controller::getValue( 'cfProfile' ),
149+
'region' => Controller::getValue( 'cfRegion' ),
150+
'version' => 'latest',
151+
]
152+
);
153+
} else {
154+
// Use the IAM role.
155+
$client = \Aws\CloudFront\CloudFrontClient::factory(
156+
[
157+
'region' => Controller::getValue( 'cfRegion' ),
158+
'version' => 'latest',
159+
]
160+
);
145161
}
146162

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-
);
163+
return $client;
164+
}
165+
166+
public function cloudfront_invalidate_all_items() : void {
167+
if ( ! Controller::getValue( 'cfDistributionID' ) ) {
168+
return;
169+
}
170+
171+
\WP2Static\WsLog::l( 'Invalidating all CloudFront items' );
172+
173+
$client = self::cloudfront_client();
155174

156175
try {
157176
$result = $client->createInvalidation(

0 commit comments

Comments
 (0)