Search in sources :

Example 1 with PermissionDeniedException

use of com.google.api.gax.rpc.PermissionDeniedException in project java-monitoring by googleapis.

the class ITVPCServiceControlTest method postJSON.

private static void postJSON(URL url, String payload) throws Exception {
    GoogleCredentials adc = GoogleCredentials.getApplicationDefault().createScoped(ImmutableList.of("https://www.googleapis.com/auth/monitoring", "https://www.googleapis.com/auth/monitoring.write", "https://www.googleapis.com/auth/cloud-platform"));
    adc.refresh();
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "Bearer " + adc.getAccessToken().getTokenValue());
    conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    conn.setRequestProperty("Accept", "application/json, */*");
    conn.setDoOutput(true);
    OutputStream body = conn.getOutputStream();
    body.write(payload.getBytes("UTF-8"));
    body.close();
    conn.getResponseMessage();
    BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) conn.getErrorStream()));
    StringBuilder response = new StringBuilder();
    for (String line; (line = reader.readLine()) != null; ) {
        response.append(line).append("\n");
    }
    if (conn.getResponseCode() == 403) {
        throw new PermissionDeniedException(response.toString(), null, GrpcStatusCode.of(io.grpc.Status.Code.PERMISSION_DENIED), false);
    } else if (conn.getResponseCode() != 200) {
        throw new RuntimeException(response.toString());
    }
    return;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedReader(java.io.BufferedReader) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) PermissionDeniedException(com.google.api.gax.rpc.PermissionDeniedException)

Example 2 with PermissionDeniedException

use of com.google.api.gax.rpc.PermissionDeniedException in project java-recommender by googleapis.

the class ListRecommendations method listRecommendations.

// List recommendations for a specified project, location, and recommender
public static void listRecommendations(String projectId, String location, String recommender) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (RecommenderClient recommenderClient = RecommenderClient.create()) {
        // / Build the request
        String parent = String.format("projects/%s/locations/%s/recommenders/%s", projectId, location, recommender);
        ListRecommendationsRequest request = ListRecommendationsRequest.newBuilder().setParent(parent).build();
        try {
            // Send the request
            ListRecommendationsPagedResponse response = recommenderClient.listRecommendations(request);
            // Print out each recommendation
            for (Recommendation responseItem : response.iterateAll()) {
                Recommendation recommendation = responseItem;
                System.out.println("Recommendation name: " + recommendation.getName());
                System.out.println("- description: " + recommendation.getDescription());
                System.out.println("- primary_impact.category: " + recommendation.getPrimaryImpact().getCategory());
                System.out.println("- state_info.state: " + recommendation.getStateInfo().getState());
                System.out.println();
            }
            // Indicate the request was successful
            System.out.println("List recommendations successful");
        } catch (PermissionDeniedException e) {
            System.out.println("Permission denied for project '" + projectId + "'. Ensure you have the appropriate permissions to list recommendations: \n" + e.toString());
        } catch (InvalidArgumentException e) {
            System.out.println(("Invalid argument for projectId. Ensure you have 'GOOGLE_CLOUD_PROJECT' set: \n" + e.toString()));
        }
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) RecommenderClient(com.google.cloud.recommender.v1beta1.RecommenderClient) PermissionDeniedException(com.google.api.gax.rpc.PermissionDeniedException) ListRecommendationsRequest(com.google.cloud.recommender.v1beta1.ListRecommendationsRequest) ListRecommendationsPagedResponse(com.google.cloud.recommender.v1beta1.RecommenderClient.ListRecommendationsPagedResponse) Recommendation(com.google.cloud.recommender.v1beta1.Recommendation)

Example 3 with PermissionDeniedException

use of com.google.api.gax.rpc.PermissionDeniedException in project java-retail by googleapis.

the class RemoveTestResources method deleteAllProducts.

public static void deleteAllProducts() throws IOException {
    System.out.println("Deleting products in process, please wait...");
    try (ProductServiceClient productServiceClient = ProductServiceClient.create()) {
        ListProductsRequest listRequest = ListProductsRequest.newBuilder().setParent(DEFAULT_CATALOG).build();
        ListProductsPagedResponse products = productServiceClient.listProducts(listRequest);
        int deleteCount = 0;
        for (Product product : products.iterateAll()) {
            DeleteProductRequest deleteRequest = DeleteProductRequest.newBuilder().setName(product.getName()).build();
            try {
                productServiceClient.deleteProduct(deleteRequest);
                deleteCount++;
            } catch (PermissionDeniedException e) {
                System.out.println("Ignore PermissionDenied in case the product does not exist " + "at time of deletion.");
            }
        }
        System.out.printf("%s products were deleted from %s%n", deleteCount, DEFAULT_CATALOG);
    }
}
Also used : DeleteProductRequest(com.google.cloud.retail.v2.DeleteProductRequest) ListProductsRequest(com.google.cloud.retail.v2.ListProductsRequest) ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) Product(com.google.cloud.retail.v2.Product) PermissionDeniedException(com.google.api.gax.rpc.PermissionDeniedException) ListProductsPagedResponse(com.google.cloud.retail.v2.ProductServiceClient.ListProductsPagedResponse)

Example 4 with PermissionDeniedException

use of com.google.api.gax.rpc.PermissionDeniedException in project java-websecurityscanner by googleapis.

the class VPCServiceControlNegativeTest method testScanConfigList.

@Test
public void testScanConfigList() throws IOException {
    // try to get the list of scan configs - it must fail
    WebSecurityScannerSettings wssSetting = getWssSettingWithCredentials();
    final String formattedParent = ProjectName.format(OUT_VPCSC_PROJECT);
    try (WebSecurityScannerClient wssClient = WebSecurityScannerClient.create(wssSetting)) {
        ListScanConfigsRequest lscRequest = ListScanConfigsRequest.newBuilder().setParent(formattedParent).build();
        ListScanConfigsPagedResponse scanConfigsList = wssClient.listScanConfigs(lscRequest);
        fail("Exception must occur for non-acceessible project scan-configs");
    } catch (PermissionDeniedException expected) {
    }
}
Also used : ListScanConfigsRequest(com.google.cloud.websecurityscanner.v1beta.ListScanConfigsRequest) WebSecurityScannerSettings(com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerSettings) WebSecurityScannerClient(com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerClient) ListScanConfigsPagedResponse(com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerClient.ListScanConfigsPagedResponse) PermissionDeniedException(com.google.api.gax.rpc.PermissionDeniedException) Test(org.junit.Test)

Example 5 with PermissionDeniedException

use of com.google.api.gax.rpc.PermissionDeniedException in project java-websecurityscanner by googleapis.

the class VPCServiceControlNegativeTest method testScanConfigCreation.

@Test
public void testScanConfigCreation() throws IOException {
    WebSecurityScannerSettings wssSetting = getWssSettingWithCredentials();
    final String formattedParent = ProjectName.format(OUT_VPCSC_PROJECT);
    try (WebSecurityScannerClient wssClient = WebSecurityScannerClient.create(wssSetting)) {
        ScanConfig scanConfig = ScanConfig.newBuilder().addAllStartingUrls(Lists.newArrayList(OUT_VPCSC_HOSTNAME)).setDisplayName(testScanConfigCreationDisplayName).build();
        CreateScanConfigRequest request = CreateScanConfigRequest.newBuilder().setParent(formattedParent).setScanConfig(scanConfig).build();
        ScanConfig responseScanConfig = wssClient.createScanConfig(request);
        fail("Exception must occur for non-accessible project");
    } catch (PermissionDeniedException expected) {
    }
}
Also used : WebSecurityScannerSettings(com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerSettings) WebSecurityScannerClient(com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerClient) ScanConfig(com.google.cloud.websecurityscanner.v1beta.ScanConfig) CreateScanConfigRequest(com.google.cloud.websecurityscanner.v1beta.CreateScanConfigRequest) PermissionDeniedException(com.google.api.gax.rpc.PermissionDeniedException) Test(org.junit.Test)

Aggregations

PermissionDeniedException (com.google.api.gax.rpc.PermissionDeniedException)7 Test (org.junit.Test)3 WebSecurityScannerClient (com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerClient)2 WebSecurityScannerSettings (com.google.cloud.websecurityscanner.v1beta.WebSecurityScannerSettings)2 AlreadyExistsException (com.google.api.gax.rpc.AlreadyExistsException)1 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 NotFoundException (com.google.api.gax.rpc.NotFoundException)1 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 CreateEntryGroupRequest (com.google.cloud.datacatalog.v1.CreateEntryGroupRequest)1 CreateEntryRequest (com.google.cloud.datacatalog.v1.CreateEntryRequest)1 DataCatalogClient (com.google.cloud.datacatalog.v1.DataCatalogClient)1 Entry (com.google.cloud.datacatalog.v1.Entry)1 EntryGroup (com.google.cloud.datacatalog.v1.EntryGroup)1 ListRecommendationsRequest (com.google.cloud.recommender.v1beta1.ListRecommendationsRequest)1 Recommendation (com.google.cloud.recommender.v1beta1.Recommendation)1 RecommenderClient (com.google.cloud.recommender.v1beta1.RecommenderClient)1 ListRecommendationsPagedResponse (com.google.cloud.recommender.v1beta1.RecommenderClient.ListRecommendationsPagedResponse)1 DeleteProductRequest (com.google.cloud.retail.v2.DeleteProductRequest)1 ListProductsRequest (com.google.cloud.retail.v2.ListProductsRequest)1 Product (com.google.cloud.retail.v2.Product)1