2222import static org .junit .Assert .fail ;
2323import static org .junit .Assume .assumeTrue ;
2424
25+ import com .google .api .gax .core .FixedCredentialsProvider ;
26+ import com .google .api .gax .grpc .InstantiatingGrpcChannelProvider ;
27+ import com .google .api .gax .rpc .PermissionDeniedException ;
28+ import com .google .auth .oauth2 .GoogleCredentials ;
29+ import com .google .cloud .Policy ;
30+ import com .google .cloud .Timestamp ;
31+ import com .google .cloud .spanner .BackupId ;
2532import com .google .cloud .spanner .DatabaseAdminClient ;
2633import com .google .cloud .spanner .DatabaseClient ;
2734import com .google .cloud .spanner .DatabaseId ;
3542import com .google .cloud .spanner .Spanner ;
3643import com .google .cloud .spanner .SpannerException ;
3744import com .google .cloud .spanner .SpannerOptions ;
45+ import com .google .longrunning .OperationsClient ;
46+ import com .google .longrunning .OperationsSettings ;
47+ import java .io .FileInputStream ;
48+ import java .io .FileNotFoundException ;
49+ import java .io .IOException ;
50+ import java .util .ArrayList ;
3851import java .util .Arrays ;
52+ import java .util .List ;
53+ import java .util .concurrent .ExecutionException ;
54+ import java .util .logging .Logger ;
3955import org .junit .After ;
4056import org .junit .Before ;
4157import org .junit .BeforeClass ;
4864@ Category (IntegrationTest .class )
4965@ RunWith (JUnit4 .class )
5066public class ITVPCNegativeTest {
67+ private static final Logger logger = Logger .getLogger (ITVPCNegativeTest .class .getName ());
5168 private static final String IN_VPCSC_TEST = System .getenv ("GOOGLE_CLOUD_TESTS_IN_VPCSC" );
5269 private static final String OUTSIDE_VPC_PROJECT =
5370 System .getenv ("GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT" );
@@ -56,6 +73,8 @@ public class ITVPCNegativeTest {
5673 private InstanceAdminClient instanceAdminClient ;
5774 private DatabaseAdminClient databaseAdminClient ;
5875 private DatabaseClient databaseClient ;
76+ private InstanceId instanceId ;
77+ private BackupId backupId ;
5978
6079 @ BeforeClass
6180 public static void setUpClass () {
@@ -70,7 +89,8 @@ public static void setUpClass() {
7089
7190 @ Before
7291 public void setUp () {
73- InstanceId instanceId = InstanceId .of (OUTSIDE_VPC_PROJECT , "nonexistent-instance" );
92+ instanceId = InstanceId .of (OUTSIDE_VPC_PROJECT , "nonexistent-instance" );
93+ backupId = BackupId .of (OUTSIDE_VPC_PROJECT , "nonexistent-instance" , "nonexistent-backup" );
7494 SpannerOptions options =
7595 SpannerOptions .newBuilder ()
7696 .setProjectId (instanceId .getProject ())
@@ -167,8 +187,170 @@ public void deniedRead() {
167187 databaseClient
168188 .singleUse ()
169189 .read ("nonexistent-table" , KeySet .all (), Arrays .asList ("nonexistent-col" ));
190+ fail ("Expected PERMISSION_DENIED SpannerException" );
191+ } catch (SpannerException e ) {
192+ checkExceptionForVPCError (e );
193+ }
194+ }
195+
196+ @ Test
197+ public void deniedCreateBackup () throws InterruptedException {
198+ try {
199+ databaseAdminClient
200+ .createBackup (instanceId .getInstance (), "newbackup-id" , "nonexistent-db" , Timestamp .now ())
201+ .get ();
202+ fail ("Expected PERMISSION_DENIED SpannerException" );
203+ } catch (ExecutionException e ) {
204+ Throwable thrown = e .getCause ();
205+ checkExceptionForVPCError ((SpannerException ) thrown );
206+ }
207+ }
208+
209+ @ Test
210+ public void deniedGetBackup () {
211+ try {
212+ databaseAdminClient .getBackup (instanceId .getInstance (), backupId .getBackup ());
213+ fail ("Expected PERMISSION_DENIED SpannerException" );
214+ } catch (SpannerException e ) {
215+ checkExceptionForVPCError (e );
216+ }
217+ }
218+
219+ @ Test
220+ public void deniedUpdateBackup () {
221+ try {
222+ databaseAdminClient .updateBackup (
223+ instanceId .getInstance (), backupId .getBackup (), Timestamp .now ());
224+ fail ("Expected PERMISSION_DENIED SpannerException" );
225+ } catch (SpannerException e ) {
226+ checkExceptionForVPCError (e );
227+ }
228+ }
229+
230+ @ Test
231+ public void deniedListBackup () {
232+ try {
233+ databaseAdminClient .listBackups (instanceId .getInstance ());
234+ fail ("Expected PERMISSION_DENIED SpannerException" );
235+ } catch (SpannerException e ) {
236+ checkExceptionForVPCError (e );
237+ }
238+ }
239+
240+ @ Test
241+ public void deniedDeleteBackup () {
242+ try {
243+ databaseAdminClient .deleteBackup (instanceId .getInstance (), backupId .getBackup ());
244+ fail ("Expected PERMISSION_DENIED SpannerException" );
245+ } catch (SpannerException e ) {
246+ checkExceptionForVPCError (e );
247+ }
248+ }
249+
250+ @ Test
251+ public void deniedRestoreDatabase () throws InterruptedException {
252+ try {
253+ databaseAdminClient
254+ .restoreDatabase (
255+ instanceId .getInstance (), "nonexistent-backup" , instanceId .getInstance (), "newdb-id" )
256+ .get ();
257+ fail ("Expected PERMISSION_DENIED SpannerException" );
258+ } catch (ExecutionException e ) {
259+ Throwable thrown = e .getCause ();
260+ checkExceptionForVPCError ((SpannerException ) thrown );
261+ }
262+ }
263+
264+ @ Test
265+ public void deniedListBackupOperationsInInstance () {
266+ try {
267+ databaseAdminClient .listBackupOperations (instanceId .getInstance ());
268+ fail ("Expected PERMISSION_DENIED SpannerException" );
170269 } catch (SpannerException e ) {
171270 checkExceptionForVPCError (e );
172271 }
173272 }
273+
274+ @ Test
275+ public void deniedListDatabaseOperationsInInstance () {
276+ try {
277+ databaseAdminClient .listDatabaseOperations (instanceId .getInstance ());
278+ fail ("Expected PERMISSION_DENIED SpannerException" );
279+ } catch (SpannerException e ) {
280+ checkExceptionForVPCError (e );
281+ }
282+ }
283+
284+ @ Test
285+ public void deniedGetBackupIamPolicy () {
286+ try {
287+ databaseAdminClient .getBackupIAMPolicy (instanceId .getInstance (), backupId .getBackup ());
288+ fail ("Expected PERMISSION_DENIED SpannerException" );
289+ } catch (SpannerException e ) {
290+ checkExceptionForVPCError (e );
291+ }
292+ }
293+
294+ @ Test
295+ public void deniedSetBackupIamPolicy () {
296+ try {
297+ Policy policy = Policy .newBuilder ().build ();
298+ databaseAdminClient .setBackupIAMPolicy (
299+ backupId .getInstanceId ().getInstance (), backupId .getBackup (), policy );
300+ fail ("Expected PERMISSION_DENIED SpannerException" );
301+ } catch (SpannerException e ) {
302+ checkExceptionForVPCError (e );
303+ }
304+ }
305+
306+ @ Test
307+ public void deniedTestBackupIamPermissions () {
308+ try {
309+ List <String > permissions = new ArrayList <>();
310+ databaseAdminClient .testBackupIAMPermissions (
311+ backupId .getInstanceId ().getInstance (), backupId .getBackup (), permissions );
312+ fail ("Expected PERMISSION_DENIED SpannerException" );
313+ } catch (SpannerException e ) {
314+ checkExceptionForVPCError (e );
315+ }
316+ }
317+
318+ @ Test
319+ public void deniedCancelBackupOperation () {
320+ try {
321+ databaseAdminClient .cancelOperation (backupId .getName () + "/operations/nonexistentop" );
322+ fail ("Expected PERMISSION_DENIED SpannerException" );
323+ } catch (SpannerException e ) {
324+ checkExceptionForVPCError (e );
325+ }
326+ }
327+
328+ @ Test
329+ public void deniedGetBackupOperation () {
330+ try {
331+ databaseAdminClient .getOperation (backupId .getName () + "/operations/nonexistentop" );
332+ fail ("Expected PERMISSION_DENIED SpannerException" );
333+ } catch (SpannerException e ) {
334+ checkExceptionForVPCError (e );
335+ }
336+ }
337+
338+ @ Test
339+ public void deniedListBackupOperations () throws FileNotFoundException , IOException {
340+ try (OperationsClient client =
341+ OperationsClient .create (
342+ OperationsSettings .newBuilder ()
343+ .setTransportChannelProvider (InstantiatingGrpcChannelProvider .newBuilder ().build ())
344+ .setEndpoint ("spanner.googleapis.com:443" )
345+ .setCredentialsProvider (
346+ FixedCredentialsProvider .create (
347+ GoogleCredentials .fromStream (
348+ new FileInputStream (System .getenv ("GOOGLE_APPLICATION_CREDENTIALS" )))))
349+ .build ())) {
350+ client .listOperations (backupId .getName () + "/operations" , "" );
351+ fail ("Expected PermissionDeniedException" );
352+ } catch (PermissionDeniedException e ) {
353+ assertThat (e .getMessage ()).contains ("Request is prohibited by organization's policy" );
354+ }
355+ }
174356}
0 commit comments