/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014, Groupon, Inc.
 *
 * 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.
 */

package com.groupon.jenkins.github.services;

import com.groupon.jenkins.github.DeployKeyPair;
import com.groupon.jenkins.mongo.MongoRepository;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import java.io.IOException;

public class GithubDeployKeyRepository extends MongoRepository {
    public void put(String url,DeployKeyPair keyPair) throws IOException {
        BasicDBObject doc = new BasicDBObject("public_key", keyPair.publicKey).append("private_key",keyPair.privateKey ).append("repo_url", url);
        getCollection().insert(doc);
    }
    protected DBCollection getCollection() {
        return getDatastore().getDB().getCollection("deploy_keys");
    }

    public DeployKeyPair get(String repoUrl) {
        BasicDBObject query = new BasicDBObject("repo_url", repoUrl);
        DBObject keyPair = getCollection().findOne(query);
        return new DeployKeyPair((String)keyPair.get("public_key"),(String)keyPair.get("private_key"));
    }

    public boolean hasDeployKey(String repoUrl) {
        BasicDBObject query = new BasicDBObject("repo_url", repoUrl);
        return getCollection().getCount(query) == 1;
    }
}

@@ -1378,24 +1378,51 @@
 b.DBObject;%0A
+import hudson.util.Secret;%0A
 import java.
@@ -1437,16 +1437,16 @@
 eption;%0A
-
 %0Apublic 
@@ -1638,16 +1638,24 @@
 c_key%22, 
+encrypt(
 keyPair.
@@ -1664,16 +1664,17 @@
 blicKey)
+)
 .append(
@@ -1687,16 +1687,24 @@
 te_key%22,
+encrypt(
 keyPair.
@@ -1713,17 +1713,17 @@
 ivateKey
- 
+)
 ).append
@@ -2113,16 +2113,24 @@
 KeyPair(
+decrypt(
 (String)
@@ -2154,17 +2154,27 @@
 ic_key%22)
-,
+),decrypt( 
 (String)
@@ -2200,16 +2200,17 @@
 e_key%22))
+)
 ;%0A    %7D%0A
@@ -2322,32 +2322,32 @@
 url%22, repoUrl);%0A
-
         return g
@@ -2389,10 +2389,226 @@
 ;%0A    %7D%0A
+    private String encrypt(String value) %7B%0A        return Secret.fromString(value).getEncryptedValue();%0A    %7D%0A%0A    private  String decrypt(String value)%7B%0A        return Secret.fromString(value).getPlainText();%0A    %7D%0A
 %7D%0A

Use Jenkins Secret to encrypt/decrypt deploy keys