Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add audit logs for oauth application management #2104

Merged
merged 2 commits into from
Jul 26, 2023
Merged

Conversation

piraveena
Copy link
Contributor

@piraveena piraveena commented Jun 22, 2023

Proposed changes in this pull request

Fixes part of wso2/product-is#5037

This PR adds new audit logs for oauth application management component.

To enable these audit logs, we need to add below config in the deployment.toml file

By adding below config, you can enable new audit logs only for application management component, and the legacy audit logs will be available for other components.

[system.parameter]
disableLegacyAuditLogsInAppMgt=true

By adding below config, you can enable new audit logs , and the legacy audit logs will not available for other components. These new audit logs are currently inprogress only for application-mgt component

[system.parameter]
disableLegacyAuditLogs=true

Sample audit logs

Delete OAuth application

{
   "id":"480f5d52-509e-46d3-ba67-4a402b6c1f41",
   "recordedAt":"2023-07-13T09:45:31.556519Z",
   "requestId":"12881a35-b2d8-470a-b507-be0624788624",
   "initiatorId":"708a5bf2-7252-4120-ad3f-6e8cdb40a0b5",
   "initiatorType":"USER",
   "targetId":"kqAE7cOqJCg8V50lY3YgOJ1ce1Ma",
   "targetType":"APPLICATION",
   "action":"DELETE OAUTH APPLICATION"
}


{
   "id":"d1e2ac7f-a8a8-4278-9bd4-13e6b4d4a52c",
   "recordedAt":"2023-07-13T09:45:31.648714Z",
   "requestId":"12881a35-b2d8-470a-b507-be0624788624",
   "initiatorId":"708a5bf2-7252-4120-ad3f-6e8cdb40a0b5",
   "initiatorType":"USER",
   "targetId":"9dce2d7b-5694-4630-aaa4-a4876e768134",
   "targetType":"APPLICATION",
   "action":"DELETE APPLICATION"
}

Create OAuth application

{
   "id":"2a12ec36-dc86-4670-b877-d13f934eea28",
   "recordedAt":"2023-07-13T09:50:17.244330Z",
   "requestId":"808eeb63-6350-45ed-a6d6-f5954730f77c",
   "initiatorId":"708a5bf2-7252-4120-ad3f-6e8cdb40a0b5",
   "initiatorType":"USER",
   "targetId":"lm5mFAW9zZWWEj1L16fPuyfMfsca",
   "targetType":"APPLICATION",
   "action":"CREATE OAUTH APPLICATION",
   "data":{
      "scopeValidators":[
         
      ],
      "idTokenExpiryTime":3600.0,
      "bypassClientCredentials":true,
      "type":"oAuthAppDO",
      "refreshTokenExpiryTime":86400.0,
      "tokenRevocationWithIDPSessionTerminationEnabled":true,
      "callbackUrl":"https://myapp.io/login",
      "pkceSupportPlain":false,
      "tokenBindingValidationEnabled":false,
      "id":0.0,
      "state":"ACTIVE",
      "renewRefreshTokenEnabled":"true",
      "oauthConsumerSecret":"2x****************************************Qa",
      "applicationName":"SPA",
      "userAccessTokenExpiryTime":3600.0,
      "oauthVersion":"OAuth-2.0",
      "grantTypes":"authorization_code refresh_token",
      "idTokenEncryptionEnabled":false,
      "applicationAccessTokenExpiryTime":3600.0,
      "pkceMandatory":true,
      "requestObjectSignatureValidationEnabled":false,
      "tokenBindingType":"sso-session",
      "oauthConsumerKey":"lm5mFAW9zZWWEj1L16fPuyfMfsca",
      "tokenType":"Default"
   }
}
{
   "id":"31ee6085-af93-4251-a197-1570a1683847",
   "recordedAt":"2023-07-13T09:50:17.632364Z",
   "requestId":"808eeb63-6350-45ed-a6d6-f5954730f77c",
   "initiatorId":"708a5bf2-7252-4120-ad3f-6e8cdb40a0b5",
   "initiatorType":"USER",
   "targetId":"a67f1b13-d192-4850-a81e-ee23e7500f4b",
   "targetType":"APPLICATION",
   "action":"CREATE APPLICATION",
   "data":{
      "spProperties":[
         {
            "name":"useUserIdForDefaultSubject",
            "value":"true"
         },
         {
            "displayName":"Is Management Application",
            "name":"isManagementApp",
            "value":"false"
         },
         {
            "displayName":"Is B2B Self Service Application",
            "name":"isB2BSelfServiceApp",
            "value":"false"
         },
         {
            "displayName":"DOMAIN_IN_ROLES",
            "name":"USE_DOMAIN_IN_ROLES",
            "value":"true"
         },
         {
            "displayName":"Skip Logout Consent",
            "name":"skipLogoutConsent",
            "value":"true"
         },
         {
            "displayName":"Skip Consent",
            "name":"skipConsent",
            "value":"false"
         },
         {
            "displayName":"Use External Consent Page",
            "name":"useExternalConsentPage",
            "value":"false"
         },
         {
            "displayName":"jwksURI",
            "name":"jwksURI",
            "value":""
         },
         {
            "displayName":"Template Id",
            "name":"templateId",
            "value":"6a90e4b0-fbff-42d7-bfde-1efd98f07cd7"
         }
      ],
      "inboundAuthenticationConfig":{
         "inboundAuthenticationRequestConfigs":[
            {
               "inboundAuthKey":"lm5mFAW9zZWWEj1L16fPuyfMfsca",
               "inboundAuthType":"oauth2",
               "properties":[
                  {
                     "advanced":false,
                     "groupId":0.0,
                     "displayOrder":0.0,
                     "subProperties":[
                        
                     ],
                     "required":false,
                     "name":"oauthConsumerSecret",
                     "options":[
                        
                     ],
                     "value":"2x****************************************Qa",
                     "confidential":false
                  }
               ]
            }
         ]
      },
      "managementApp":false,
      "templateId":"6a90e4b0-fbff-42d7-bfde-1efd98f07cd7",
      "b2BSelfServiceApp":false,
      "applicationResourceId":"a67f1b13-d192-4850-a81e-ee23e7500f4b",
      "localAndOutBoundAuthenticationConfig":{
         "enableAuthorization":false,
         "useTenantDomainInLocalSubjectIdentifier":false,
         "skipLogoutConsent":true,
         "skipConsent":false,
         "alwaysSendBackAuthenticatedListOfIdPs":false,
         "authenticationSteps":[
            
         ],
         "useUserstoreDomainInRoles":true,
         "useExternalConsentPage":false,
         "useUserstoreDomainInLocalSubjectIdentifier":false,
         "authenticationType":"default"
      },
      "discoverable":false,
      "applicationID":48.0,
      "applicationName":"SPA",
      "saasApp":false
   }
}

Update OAuth application protocol configs

{
   "id":"3f3dd3c8-2eaf-4706-8c8c-88dbf807918a",
   "recordedAt":"2023-07-13T09:53:18.626265Z",
   "requestId":"cd036e15-90fd-4b59-b462-2423456b5d34",
   "initiatorId":"708a5bf2-7252-4120-ad3f-6e8cdb40a0b5",
   "initiatorType":"USER",
   "targetId":"lm5mFAW9zZWWEj1L16fPuyfMfsca",
   "targetType":"APPLICATION",
   "action":"UPDATE OAUTH APPLICATION",
   "data":{
      "idTokenEncryptionMethod":"null",
      "scopeValidators":[
         
      ],
      "idTokenExpiryTime":3600.0,
      "bypassClientCredentials":true,
      "type":"oAuthAppDO",
      "refreshTokenExpiryTime":86400.0,
      "idTokenEncryptionAlgorithm":"null",
      "tokenRevocationWithIDPSessionTerminationEnabled":true,
      "audiences":[
         
      ],
      "callbackUrl":"https://myapp.io/login",
      "pkceSupportPlain":false,
      "tokenBindingValidationEnabled":false,
      "id":44.0,
      "state":"ACTIVE",
      "renewRefreshTokenEnabled":"true",
      "oauthConsumerSecret":"2x****************************************Qa",
      "applicationName":"SPA",
      "userAccessTokenExpiryTime":3600.0,
      "oauthVersion":"OAuth-2.0",
      "grantTypes":"authorization_code refresh_token implicit",
      "idTokenEncryptionEnabled":false,
      "applicationAccessTokenExpiryTime":3600.0,
      "pkceMandatory":true,
      "requestObjectSignatureValidationEnabled":false,
      "tokenBindingType":"sso-session",
      "oauthConsumerKey":"lm5mFAW9zZWWEj1L16fPuyfMfsca",
      "tokenType":"Default"
   }
}
{
   "id":"66253827-ce63-4e0e-ad8d-23058de5be81",
   "recordedAt":"2023-07-13T09:53:18.696009Z",
   "requestId":"cd036e15-90fd-4b59-b462-2423456b5d34",
   "initiatorId":"708a5bf2-7252-4120-ad3f-6e8cdb40a0b5",
   "initiatorType":"USER",
   "targetId":"a67f1b13-d192-4850-a81e-ee23e7500f4b",
   "targetType":"APPLICATION",
   "action":"UPDATE APPLICATION",
   "data":{
      "owner":{
         "userStoreDomain":"PRIMARY",
         "loggableUserId":"708a5bf2-7252-4120-ad3f-6e8cdb40a0b5",
         "userName":"a***n",
         "tenantDomain":"carbon.super"
      },
      "outboundProvisioningConfig":{
         "provisioningIdentityProviders":[
            
         ]
      },
      "jwksUri":"",
      "spProperties":[
         {
            "name":"useUserIdForDefaultSubject",
            "value":"true"
         },
         {
            "displayName":"Is Management Application",
            "name":"isManagementApp",
            "value":"false"
         },
         {
            "displayName":"Is B2B Self Service Application",
            "name":"isB2BSelfServiceApp",
            "value":"false"
         },
         {
            "displayName":"DOMAIN_IN_ROLES",
            "name":"USE_DOMAIN_IN_ROLES",
            "value":"true"
         },
         {
            "displayName":"Skip Logout Consent",
            "name":"skipLogoutConsent",
            "value":"true"
         },
         {
            "displayName":"Skip Consent",
            "name":"skipConsent",
            "value":"false"
         },
         {
            "displayName":"Use External Consent Page",
            "name":"useExternalConsentPage",
            "value":"false"
         },
         {
            "displayName":"jwksURI",
            "name":"jwksURI",
            "value":""
         },
         {
            "displayName":"Template Id",
            "name":"templateId",
            "value":"6a90e4b0-fbff-42d7-bfde-1efd98f07cd7"
         }
      ],
      "inboundAuthenticationConfig":{
         "inboundAuthenticationRequestConfigs":[
            {
               "inboundAuthKey":"lm5mFAW9zZWWEj1L16fPuyfMfsca",
               "inboundAuthType":"oauth2",
               "properties":[
                  {
                     "advanced":false,
                     "groupId":0.0,
                     "displayOrder":0.0,
                     "subProperties":[
                        
                     ],
                     "required":false,
                     "name":"oauthConsumerSecret",
                     "options":[
                        
                     ],
                     "value":"2x****************************************Qa",
                     "confidential":false
                  }
               ]
            }
         ]
      },
      "requestPathAuthenticatorConfigs":[
         
      ],
      "managementApp":false,
      "templateId":"6a90e4b0-fbff-42d7-bfde-1efd98f07cd7",
      "tenantDomain":"carbon.super",
      "b2BSelfServiceApp":false,
      "applicationResourceId":"a67f1b13-d192-4850-a81e-ee23e7500f4b",
      "permissionAndRoleConfig":{
         "permissions":[
            
         ],
         "idpRoles":[
            
         ],
         "roleMappings":[
            
         ]
      },
      "localAndOutBoundAuthenticationConfig":{
         "enableAuthorization":false,
         "useTenantDomainInLocalSubjectIdentifier":false,
         "skipLogoutConsent":true,
         "skipConsent":false,
         "alwaysSendBackAuthenticatedListOfIdPs":false,
         "authenticationSteps":[
            {
               "subjectStep":true,
               "attributeStep":true,
               "localAuthenticatorConfigs":[
                  {
                     "valid":true,
                     "displayName":"basicauth",
                     "name":"BasicAuthenticator",
                     "enabled":false,
                     "properties":[
                        
                     ]
                  }
               ],
               "federatedIdentityProviders":[
                  
               ],
               "stepOrder":1.0
            }
         ],
         "useUserstoreDomainInRoles":true,
         "useExternalConsentPage":false,
         "useUserstoreDomainInLocalSubjectIdentifier":false,
         "authenticationType":"default"
      },
      "discoverable":false,
      "inboundProvisioningConfig":{
         "dumbMode":false,
         "provisioningEnabled":false
      },
      "applicationID":48.0,
      "applicationName":"SPA",
      "claimConfig":{
         "localClaimDialect":true,
         "alwaysSendMappedLocalSubjectId":false,
         "claimMappings":[
            
         ],
         "idpClaims":[
            
         ],
         "spClaimDialects":[
            
         ]
      },
      "saasApp":false
   }
}

Regenerate client secret

{
   "id":"5e1cd058-250c-47a5-9dc5-25e64718a9a3",
   "recordedAt":"2023-07-13T09:58:25.783740Z",
   "requestId":"d1c5c430-4cda-4ccf-91ce-11d052301ee0",
   "initiatorId":"708a5bf2-7252-4120-ad3f-6e8cdb40a0b5",
   "initiatorType":"USER",
   "targetId":"gznRaOTMwh9Fbm20tuYV1PTfTUQa",
   "targetType":"APPLICATION",
   "action":"REGENERATE CLIENT SECRET"
}

Revoke OAuth application

{
   "id":"23506095-edc3-44cd-b8ec-8b7dcd9410d3",
   "recordedAt":"2023-07-13T10:11:23.117203Z",
   "requestId":"1e7d04aa-fed3-479a-b834-1b0e7db7c7b4",
   "initiatorId":"708a5bf2-7252-4120-ad3f-6e8cdb40a0b5",
   "initiatorType":"USER",
   "targetId":"gznRaOTMwh9Fbm20tuYV1PTfTUQa",
   "targetType":"APPLICATION",
   "action":"UPDATE APP STATE",
   "data":{
      "state":"REVOKED"
   }
}

Activate the OAuth application

When activating a revoked oauth application, client secret is also regenerated

{
   "id":"7837a1ae-baed-4632-8b6e-3e65beeed14d",
   "recordedAt":"2023-07-13T13:37:36.430025Z",
   "requestId":"1d1d8ce6-b814-4648-9e16-6f2f499fe523",
   "initiatorId":"708a5bf2-7252-4120-ad3f-6e8cdb40a0b5",
   "initiatorType":"USER",
   "targetId":"gznRaOTMwh9Fbm20tuYV1PTfTUQa",
   "targetType":"APPLICATION",
   "action":"UPDATE APP STATE",
   "data":{
      "state":"ACTIVE"
   }
}


{
   "id":"c86d4300-b07b-427c-8afa-5bac9beab300",
   "recordedAt":"2023-07-13T13:37:36.435484Z",
   "requestId":"1d1d8ce6-b814-4648-9e16-6f2f499fe523",
   "initiatorId":"708a5bf2-7252-4120-ad3f-6e8cdb40a0b5",
   "initiatorType":"USER",
   "targetId":"gznRaOTMwh9Fbm20tuYV1PTfTUQa",
   "targetType":"APPLICATION",
   "action":"REGENERATE CLIENT SECRET"
}

When should this PR be merged

[Please describe any preconditions that need to be addressed before we
can merge this pull request.]

Follow up actions

[List any possible follow-up actions here; for instance, testing data
migrations, software that we need to install on staging and production
environments.]

Checklist (for reviewing)

General

  • Is this PR explained thoroughly? All code changes must be accounted for in the PR description.
  • Is the PR labeled correctly?

Functionality

  • Are all requirements met? Compare implemented functionality with the requirements specification.
  • Does the UI work as expected? There should be no Javascript errors in the console; all resources should load. There should be no unexpected errors. Deliberately try to break the feature to find out if there are corner cases that are not handled.

Code

  • Do you fully understand the introduced changes to the code? If not ask for clarification, it might uncover ways to solve a problem in a more elegant and efficient way.
  • Does the PR introduce any inefficient database requests? Use the debug server to check for duplicate requests.
  • Are all necessary strings marked for translation? All strings that are exposed to users via the UI must be marked for translation.

Tests

  • Are there sufficient test cases? Ensure that all components are tested individually; models, forms, and serializers should be tested in isolation even if a test for a view covers these components.
  • If this is a bug fix, are tests for the issue in place? There must be a test case for the bug to ensure the issue won’t regress. Make sure that the tests break without the new code to fix the issue.
  • If this is a new feature or a significant change to an existing feature? has the manual testing spreadsheet been updated with instructions for manual testing?

Security

  • Confirm this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets.
  • Are all UI and API inputs run through forms or serializers?
  • Are all external inputs validated and sanitized appropriately?
  • Does all branching logic have a default case?
  • Does this solution handle outliers and edge cases gracefully?
  • Are all external communications secured and restricted to SSL?

Documentation

  • Are changes to the UI documented in the platform docs? If this PR introduces new platform site functionality or changes existing ones, the changes should be documented.
  • Are changes to the API documented in the API docs? If this PR introduces new API functionality or changes existing ones, the changes must be documented.
  • Are reusable components documented? If this PR introduces components that are relevant to other developers (for instance a mixin for a view or a generic form) they should be documented in the Wiki.

@piraveena piraveena marked this pull request as ready for review July 14, 2023 03:47
@piraveena piraveena changed the title Add audit logs for oauth app-mgt Add audit logs for oauth application management Jul 14, 2023
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2023, WSO2 Inc. (https://www.wso2.org) All Rights Reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update the license header to LLC one. Check other places as well

private final String eventDescription;

private EventCatalog(String eventId, String eventDescription) {
this.eventId = eventId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing new line after the method signature
check other places as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this class and moved to string constant. Using enum might give some additional restrictions. In future, we need to have an osgi service to register all the suppported events. Using that, we can have some REST API to get the supported events

return;
}
String secret = oauthApp.get("oauthConsumerSecret").toString();
String maskedSecret = secret.replaceAll(MASKING_REGEX, MASKING_CHARACTER);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@piraveena piraveena force-pushed the audit-logs-imp branch 2 times, most recently from 433100e to d446651 Compare July 20, 2023 09:01
@jenkins-is-staging
Copy link

PR builder started
Link: https://github.com/wso2/product-is/actions/runs/5654312972

@jenkins-is-staging
Copy link

PR builder completed
Link: https://github.com/wso2/product-is/actions/runs/5654312972
Status: cancelled

@jenkins-is-staging
Copy link

PR builder started
Link: https://github.com/wso2/product-is/actions/runs/5654380136

@jenkins-is-staging
Copy link

PR builder completed
Link: https://github.com/wso2/product-is/actions/runs/5654380136
Status: success

Copy link

@jenkins-is-staging jenkins-is-staging left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving the pull request based on the successful pr build https://github.com/wso2/product-is/actions/runs/5654380136

@piraveena
Copy link
Contributor Author

Repo build https://github.com/wso2/product-is/suites/14543169080/artifacts/823235542 and integration tests builds are successful

Comment on lines +431 to +450
private static void maskAppOwnerUsername(JSONObject oauthAppJSONObject) throws IdentityException {

JSONObject appOwner = oauthAppJSONObject.optJSONObject("appOwner");
if (!LoggerUtils.isLogMaskingEnable || appOwner == null) {
return;
}
String username = (String) appOwner.get("userName");
if (StringUtils.isNotBlank(username)) {
appOwner.put("userName", LoggerUtils.getMaskedContent(username));
}
}

private static void maskClientSecret(JSONObject oauthApp) {

if (oauthApp.get("oauthConsumerSecret") == null) {
return;
}
String secret = oauthApp.get("oauthConsumerSecret").toString();
oauthApp.put("oauthConsumerSecret", LoggerUtils.getMaskedContent(secret));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we merge these two and have a single method like maskField?

eg: maskField(JSONObject jsonObject, String... path)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and call that in the maskSPData method like;
maskField(oauthAppJSONObject, "appOwner", "userName");
maskField(oauthAppJSONObject, "oauthConsumerSecret");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can have a single method, but still the logic will be different. For example, clientSecret is available as first class attribute in the path. But username is available as "appOwner:{"username":"xxx}". The path of the masking object is not a flat one. In some cases, it is nested.

Comment on lines 966 to 970
String tenantDomain = IdentityTenantUtil.getTenantDomainFromContext();
if (StringUtils.isBlank(tenantDomain)) {
tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
}
return tenantDomain;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String tenantDomain = IdentityTenantUtil.getTenantDomainFromContext();
if (StringUtils.isBlank(tenantDomain)) {
tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
}
return tenantDomain;
return Optional.ofNullable(IdentityTenantUtil.getTenantDomainFromContext())
.filter(StringUtils::isNotBlank)
.orElseGet(() -> PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain());
}

@jenkins-is-staging
Copy link

PR builder started
Link: https://github.com/wso2/product-is/actions/runs/5668712309

@jenkins-is-staging
Copy link

PR builder completed
Link: https://github.com/wso2/product-is/actions/runs/5668712309
Status: success

Copy link

@jenkins-is-staging jenkins-is-staging left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving the pull request based on the successful pr build https://github.com/wso2/product-is/actions/runs/5668712309

@piraveena
Copy link
Contributor Author

Repo PR builder is failing with the error:

Error: [ERROR] error reading /home/runner/.m2/repository/org/wso2/orbit/com/github/dblock/waffle/waffle-jna/1.6.wso2v6/waffle-jna-1.6.wso2v6.jar; Invalid CEN header (invalid zip64 extra data field size)
``` This should be due to java version. Since the PR builder in product-is is passing (it builds the oauth component with testcases), will merge this PR

@piraveena piraveena merged commit 67f2e33 into master Jul 26, 2023
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants