public static List getPrincipalsFromPolicy()

in aws-codeguruprofiler-profilinggroup/src/main/java/software/amazon/codeguruprofiler/profilinggroup/AgentPermissionHelper.java [26:46]


    public static List<String> getPrincipalsFromPolicy(AmazonWebServicesClientProxy proxy, String pgName) {
        GetPolicyRequest getPolicyRequest = GetPolicyRequest.builder().profilingGroupName(pgName).build();
        GetPolicyResponse getPolicyResponse = proxy.injectCredentialsAndInvokeV2(getPolicyRequest, profilerClient::getPolicy);
        String policyInJson = getPolicyResponse.policy();

        if (policyInJson == null || policyInJson.isEmpty()) return emptyList();

        try {
            // An example policy returned from the response can be found in [AgentPermissionHelperTest]
            Map<String, List<Map<String, Map<String, Object>>>> policyMap = objectMapper.readValue(policyInJson, Map.class);
            Object principals = policyMap.get("Statement").get(0).get("Principal").get("AWS");

            if (principals instanceof String) {
                return singletonList((String) principals);
            } else {
                return (List<String>) principals;
            }
        } catch (Exception e) {
            throw new CfnInternalFailureException(e);
        }
    }