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

[JENKINS-55694] Log audit events for user property changes #19

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<properties>
<revision>1.0</revision>
<changelist>-SNAPSHOT</changelist>
<jenkins.version>2.161</jenkins.version>
<jenkins.version>2.169-SNAPSHOT</jenkins.version>
<java.level>8</java.level>
<log4j.version>2.11.1</log4j.version>
<log4j-audit.version>1.0.1</log4j-audit.version>
Expand Down Expand Up @@ -161,6 +161,10 @@
<id>repo.jenkins-ci.org</id>
<url>https://repo.jenkins-ci.org/public/</url>
</repository>
<repository>
<id>jenkins-2.169-snapshot</id>
<url>https://repo.jenkins-ci.org/snapshots/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.jenkins.plugins.audit.listeners;

/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

import hudson.Extension;
import hudson.ExtensionList;
import hudson.model.UserProperty;

import io.jenkins.plugins.audit.event.CreateKey;
import jenkins.security.ApiTokenProperty;
import jenkins.security.ApiTokenPropertyListener;
import org.apache.logging.log4j.audit.LogEventFactory;

import javax.annotation.Nonnull;

/**
* Listener notified of api token key creation events.
*/
@Extension
public class ApiKeyCreationListener extends ApiTokenPropertyListener {

/**
* Fired when a new user property has been created.
*
* @param username the user
* @param value property that was newly created.
*
*/
@Override
public void onCreated(@Nonnull String username, @Nonnull UserProperty value) {
if (value instanceof ApiTokenProperty) {
CreateKey user = LogEventFactory.getEvent(CreateKey.class);

user.setUserId(username);
user.logEvent();
}
}

/**
* Returns a registered {@link ApiKeyCreationListener} instance.
*/
public static ExtensionList<ApiKeyCreationListener> getInstance() { return ExtensionList.lookup(ApiKeyCreationListener.class); }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.jenkins.plugins.audit.listeners;

/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

import hudson.Extension;
import hudson.ExtensionList;
import hudson.model.UserProperty;

import io.jenkins.plugins.audit.event.DeleteKey;
import jenkins.security.ApiTokenProperty;
import jenkins.security.ApiTokenPropertyListener;
import org.apache.logging.log4j.audit.LogEventFactory;

import javax.annotation.Nonnull;

/**
* Listener notified of api token key deletion or revocation events.
*/
@Extension
public class ApiKeyDeletionListener extends ApiTokenPropertyListener {

/**
* Fired when an api token has been revoked
*
* @param username id of the user
* @param value api token property of the user
*
*/
@Override
public void onDeleted(@Nonnull String username, @Nonnull UserProperty value) {
if (value instanceof ApiTokenProperty) {
DeleteKey user = LogEventFactory.getEvent(DeleteKey.class);

user.setUserId(username);
user.logEvent();
}
}

/**
* Returns a registered {@link ApiKeyDeletionListener} instance.
*/
public static ExtensionList<ApiKeyDeletionListener> getInstance() { return ExtensionList.lookup(ApiKeyDeletionListener.class); }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.jenkins.plugins.audit.listeners;

/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

import hudson.security.PasswordPropertyListener;
import io.jenkins.plugins.audit.event.UpdatePassword;
import org.apache.logging.log4j.audit.LogEventFactory;

import hudson.Extension;
import hudson.ExtensionList;

import javax.annotation.Nonnull;

/**
* Listener which logs password-update audit events.
*/
@Extension
public class UserPasswordLogListener extends PasswordPropertyListener {

/**
* Fired when a user password property has been updated and will log the event.
*
* @param username the user
* @param oldValue old property of the user
* @param newValue new property of the user
*
*/
@Override
public void onChanged(@Nonnull String username, @Nonnull Object oldValue, @Nonnull Object newValue) {
UpdatePassword user = LogEventFactory.getEvent(UpdatePassword.class);

user.setUserId(username);
user.logEvent();
}

/**
* Returns a registered {@link UserPasswordLogListener} instance.
*/
public static ExtensionList<UserPasswordLogListener> getInstance() { return ExtensionList.lookup(UserPasswordLogListener.class); }

}
33 changes: 23 additions & 10 deletions src/main/resources/catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,45 @@
} ]
}, {
"id" : 2,
"name" : "updateKey",
"displayName" : "Update Key",
"description" : "User updates API key(s)",
"name" : "createKey",
"displayName" : "Create Key",
"description" : "User creates a new API key",
"aliases" : [ ],
"attributes" : [ {
"name" : "userId",
"required" : true
"required" : false
}, {
"name" : "timestamp",
"required" : true
"required" : false
} ]
}, {
"id" : 3,
"name" : "deleteKey",
"displayName" : "Delete Key",
"description" : "User deletes or revokes an existing API key",
"aliases" : [ ],
"attributes" : [ {
"name" : "userId",
"required" : false
}, {
"name" : "timestamp",
"required" : false
} ]
}, {
"id" : 4,
"name" : "updatePassword",
"displayName" : "Update Password",
"description" : "User updates password",
"aliases" : [ ],
"attributes" : [ {
"name" : "userId",
"required" : true
"required" : false
}, {
"name" : "timestamp",
"required" : true
"required" : false
} ]
}, {
"id" : 4,
"id" : 5,
"name" : "logout",
"displayName" : "Logout",
"description" : "User Logout",
Expand All @@ -54,7 +67,7 @@
"required" : false
} ]
}, {
"id" : 5,
"id" : 6,
"name" : "createUser",
"displayName" : "Create User",
"description" : "Create User",
Expand All @@ -67,7 +80,7 @@
"required" : false
} ]
}, {
"id" : 6,
"id" : 7,
"name" : "buildStart",
"displayName" : "Build Start",
"description" : "Start of the Build",
Expand Down
Loading