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

Automatically mask credentials from build log #2

Merged
merged 7 commits into from Nov 16, 2016

Conversation

tobilarscheid
Copy link
Contributor

The vault plugin should mask any secrets obtained through it from the build login see JIRA Issue. This is already done by the regular jenkins credentials binding plugin - implementation is therefore heavily inspired by how it is done there.

Important: I was not able to run the tests, as it requires a properly initialized local Vault Instance. I would propose to rather mock the real Vault Instance for the Unit Tests to make them not only faster but also easier to run for contributors. Also, my change is actually breaking the test as the test asserts echo'ed output in the log.

@ptierno
Copy link
Contributor

ptierno commented Nov 7, 2016

I am going to review this. And I agree on mocking the vault calls. I'll look into implementing that in the very near future.

@tobilarscheid
Copy link
Contributor Author

tobilarscheid commented Nov 8, 2016

I took the time and ran the tests locally. I fixed them, they now assert to find echo **** in the log and assert to not find the secret value in the log. From my point of view this feature is now ready to merge. Better tests including a mocked vault would belong to a different PR in my opinion.

@ptierno
Copy link
Contributor

ptierno commented Nov 14, 2016

@tobilarscheid I am getting the following error when using the plugin in a pipeline script:

java.io.NotSerializableException: com.datapipe.jenkins.vault.VaultBuildWrapper$1
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:569)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
    at java.util.TreeMap.writeObject(TreeMap.java:2438)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:271)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
    at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
    at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:132)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:371)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:355)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:309)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$000(CpsThreadGroup.java:77)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:186)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:184)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:47)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:112)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: an exception which occurred:
    in field values
    in field contextVariables
    in field threads
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@142f6991
Finished: FAILURE

@tobilarscheid
Copy link
Contributor Author

@ptierno I assume the problem is in the anonymous ConsoleLogFilter I defined, I will update it to be serializable. (Also, it might be cleaner to put that into a separate class anyways.)

@tobilarscheid
Copy link
Contributor Author

@ptierno I updated it. Can you please run it in your Jenkins instance to see if it works?

@ptierno
Copy link
Contributor

ptierno commented Nov 14, 2016

@tobilarscheid I am still getting the same exception when using in a pipeline script

@tobilarscheid
Copy link
Contributor Author

tobilarscheid commented Nov 14, 2016

Hi @ptierno ,

let me try to understand the issue. I just built a Version of the plugin like so:
mvn clean package

I set up a fresh Jenkins install (Version 2.19.2) with the latest version of the workflow / pipeline plugin. I uploaded the freshly built target/hashicorp-vault-plugin.hpi through the jenkins plugin management interface.

I then ran this pipeline:

node{
    wrap([$class: 'VaultBuildWrapper', vaultSecrets: [
                [$class: 'VaultSecret', path: "secret/my", secretValues: [
                        [$class: 'VaultSecretValue', envVar: 'password', vaultKey: "password"]]]]]) {
        echo env.password
        }
}

The pipeline successfully finishes and prints what I would expect it to print:


[Pipeline] echo
****

What can we do to sort out why it doesn't work for you?

@ptierno
Copy link
Contributor

ptierno commented Nov 14, 2016

@tobilarscheid i was using mvn hpi:run to run jenkins. let me check on another jenkins instance.

@tobilarscheid
Copy link
Contributor Author

For me, it even works in the jenkins started from mvn hpi:run. It is jenkins version 1.625.3 though and I had to manually install the pipeline plugin, but then the pipeline I just posted runs without complaints.

@ptierno
Copy link
Contributor

ptierno commented Nov 14, 2016

@tobilarscheid strange. let me look into it a bit more. i want to have this merged and a release pushed asap. ill keep you updated

@tobilarscheid
Copy link
Contributor Author

Thank you. If I can be helpful in any way just let me know. We should definitely make sure every thing works fine before we release it.

* Created by tobiaslarscheid on 14.11.16.
*/
public class MaskingConsoleLogFilter extends ConsoleLogFilter implements Serializable{
final Run<?, ?> build;
Copy link
Contributor

Choose a reason for hiding this comment

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

com.hudson.Run is not serializable. This is what is causing my errors. Change to String charsetName

Copy link
Contributor

Choose a reason for hiding this comment

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

be sure to update all refs

@@ -167,6 +164,11 @@ public void setUp(Context context, Run<?, ?> build, FilePath workspace,
}
}

@Override
public ConsoleLogFilter createLoggerDecorator(@Nonnull final Run<?, ?> build) {
return new MaskingConsoleLogFilter(build, valuesToMask);
Copy link
Contributor

Choose a reason for hiding this comment

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

first aregument should be buld.getCharset().name()

*/
public class MaskingConsoleLogFilter extends ConsoleLogFilter implements Serializable{
final Run<?, ?> build;
private List<String> valuesToMask;
Copy link
Contributor

Choose a reason for hiding this comment

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

Be sure to add a private static final long serialVersionUID (ie 1L)

@ptierno
Copy link
Contributor

ptierno commented Nov 16, 2016

@tobilarscheid see my inline comments in the code. once you make those changes we should be good to merge.

@ptierno ptierno self-assigned this Nov 16, 2016
@ptierno
Copy link
Contributor

ptierno commented Nov 16, 2016

oh, and if you have the time please cleanup unused imports ;)

@tobilarscheid
Copy link
Contributor Author

Hi Peter,

thanks for your valuable feedback! I incorporated all of it into the code.

@ptierno
Copy link
Contributor

ptierno commented Nov 16, 2016

@tobilarscheid Thanks! im going to merge this now and push release either sometime today or early tomorrow. Thanks again!

@ptierno ptierno merged commit 0781c25 into jenkinsci:master Nov 16, 2016
ptierno pushed a commit that referenced this pull request Feb 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants