aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/linaro/pubapi/LinaroPubAPIKey.java
blob: 45d5fbd0e9558855382058a06285b60b241266ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package linaro.pubapi;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import hudson.AbortException;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.ParametersAction;
import hudson.model.StringParameterValue;
import hudson.security.ACL;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.util.ListBoxModel;

import jenkins.model.Jenkins;

import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import com.cloudbees.jenkins.plugins.sshcredentials.SSHAuthenticator;

import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.SchemeRequirement;
import com.cloudbees.plugins.credentials.CredentialsMatcher;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;

import net.sf.json.JSONObject;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

// Solely to filter by "SSH connection" credentials type
import com.trilead.ssh2.Connection;

/**
 * @author Paul Sokolovsky
 */
public class LinaroPubAPIKey extends Builder {

    private static final SchemeRequirement SCHEME = new SchemeRequirement("pubapi");
    private static final CredentialsMatcher CREDENTIALS_MATCHER = CredentialsMatchers.anyOf(
            CredentialsMatchers.instanceOf(StandardUsernamePasswordCredentials.class)
    );

    public String credentialsId;

    @DataBoundConstructor
    public LinaroPubAPIKey(String credentialsId) {
        this.credentialsId = credentialsId;
    }


    @Override
    public Descriptor getDescriptor() {
        return (Descriptor) super.getDescriptor();
    }

    @Extension
    public static class Descriptor extends BuildStepDescriptor<Builder> {

        // Using public members alleviates us from creating getters/setters
        public String defaultNotBefore;
        public String defaultNotAfter;

        public Descriptor() {
            load();
        }

        @Override
        public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
            req.bindJSON(this, json.getJSONObject("linaropubapi"));
            save();
            return true;
        }

        @Override
        public boolean isApplicable(Class<? extends AbstractProject> jobType) {
            // Go bold and apply to any possible project type
            return true;
            //return FreeStyleProject.class.isAssignableFrom(jobType);
        }

        @Override
        public String getDisplayName() {
            return "Linaro Publishing API Token";
        }

        public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item project) {
            return new StandardListBoxModel().withMatching(SSHAuthenticator.matcher(Connection.class),
                    CredentialsProvider.lookupCredentials(StandardCredentials.class, project,
                            ACL.SYSTEM, SCHEME));
        }

    }

    private static StandardUsernamePasswordCredentials lookupSystemCredentials(String credentialsId) {
        return CredentialsMatchers.firstOrNull(
                CredentialsProvider
                        .lookupCredentials(StandardUsernamePasswordCredentials.class, Jenkins.getInstance(), ACL.SYSTEM,
                                SCHEME),
                CredentialsMatchers.withId(credentialsId)
        );
    }

    @Override
    public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener)
            throws InterruptedException, IOException {
        HttpClient client = new HttpClient();

        if (credentialsId == null) {
            credentialsId = "snapshots.linaro.org";
        }

        StandardUsernamePasswordCredentials cred = lookupSystemCredentials(credentialsId);
        if (cred == null) {
            throw new AbortException("Unable to get publishing credentials with ID: '" + credentialsId + "'");
        }

        String url = cred.getUsername();
        listener.getLogger().format("Requesting publishing token for %s%n", url);

        PostMethod method = new PostMethod(url);
        method.setRequestHeader("AuthToken", cred.getPassword().getPlainText());
        method.addParameter("not_valid_til", Integer.toString(Integer.parseInt(getDescriptor().defaultNotBefore) * 60));
        method.addParameter("expires", Integer.toString(Integer.parseInt(getDescriptor().defaultNotAfter) * 60));

        // Auto-retry once on some (connection) errors
        // Note: for some connection issues, there's very long timeouts, so
        // having anything more than 1 here contradicts "fail fast" approach.
        // Disable auto-retry altogether for now - note that there's autoretry
        // by default.
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler(0, false));

        String token = null;
        try {
            int statusCode = client.executeMethod(method);
            //System.out.println("Status: " + method.getStatusLine());

            if (statusCode != 201) {
                System.err.println("Method failed: " + method.getStatusLine());
                throw new AbortException("Unable to get publishing key: " + method.getStatusLine());
            }
            String loc = method.getResponseHeader("Location").getValue();
            System.out.println("Token URL: " + loc);
            token = loc.substring(loc.lastIndexOf("/") + 1);

        } catch (HttpException e) {
            System.err.println("Fatal protocol violation: " + e.getMessage());
            e.printStackTrace();
            throw new AbortException("Unable to get publishing key: " + e.getMessage());
        } catch (IOException e) {
            System.err.println("Fatal transport error for '" + url + "': " + e.getMessage());
            e.printStackTrace();
            throw new AbortException("Unable to get publishing key: " + e.getMessage());
        } finally {
            // Release the connection
            method.releaseConnection();
        }

        build.addAction(new ParametersAction(new StringParameterValue("PUBLISH_TOKEN", token)));

        Matcher m = Pattern.compile("(.+://.+?/)").matcher(url);
        if (m.find()) {
            String serverUrl = m.group(1);
            build.addAction(new ParametersAction(new StringParameterValue("PUBLISH_SERVER", serverUrl)));
        }

        return true;
    }
}