Skip to content
This repository has been archived by the owner on Feb 18, 2020. It is now read-only.

Move apikey to credential #8

Open
wants to merge 7 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
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ And In your Build configuration, Add EMLauncher Plugin to post build process and
- Added macro expansion by "TokenMacro" to description sent to EMlauncher.
Like:
${ENV ,var="description"}
- Move EMLauncher host's authentication information (host name and API key) from global settings to credentials.
- Added compatibility with Credentials Binding Plugin.

## Example Pipeline script
```
Expand All @@ -29,7 +31,7 @@ OUTPUT_FILE_NAME = "${BUILD_TARGET}.ipa"
stage('upload') {
// Upload IPA to a test site such as TestFlight.
emlauncherUploader(
hostTokenPairName: 'Sandbox',
emlauncherCredentialId: 'Sandbox_Credential',
filePath: "build/Release-iphoneos/AdHoc/${OUTPUT_FILE_NAME}",
dsymPath: "build/Release-iphoneos/AdHoc/${BUILD_TARGET}-dSYM.zip",
title: 'My test app',
Expand All @@ -39,3 +41,27 @@ stage('upload') {
)
}
```

## How to use 'Credentials Binding Plugin' at Web GUI
- Enable 'Use secret text(s) or file(s)'.
- Added 'EM launcher Host Token Pair' to 'Bindings'.
- Specifies the name of the environment variable that stores the information stored in 'Credentials'.

Then, you can use the value of the environment variable specified here by a shell script etc. after that.

## How to use 'Credentials Binding' at Pipeline script

```
stage('use_credential') {
withCredentials([[$class: 'EMLauncherCredentialsBinding', credentialsId: 'Sandbox_Credential', apiHostVariable: 'EM_HOST', apiTokenVariable: 'EM_API_TOKEN', sslEnableVariable: 'EM_SSL_ENABLE']]) {
sh '''#!/bin/bash
# EMlauncherのパッケージ一覧を取得
if [ "${EM_SSL_ENABLE}" = "true" ]; then
scheme="https"
else
scheme="http"
fi
curl ${scheme}://${EM_HOST}/api/package_list?api_key={${EM_API_TOKEN}}
'''
}
```
25 changes: 20 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
<artifactId>token-macro</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.1</version>
</dependency>
<artifactId>workflow-job</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down Expand Up @@ -113,10 +113,25 @@
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials-binding</artifactId>
<version>1.16</version>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-extensions</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>

<properties>
<httpclient.version>4.0.1</httpclient.version>
<httpclient.version>4.5.3</httpclient.version>
<jenkins.version>1.625.1</jenkins.version>
<java.level>7</java.level>
</properties>
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/emlauncher/EMLauncherCredentials.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* The MIT License
*
* Copyright (c) 2011-2012, CloudBees, Inc., Stephen Connolly.
*
* 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 emlauncher;

import com.cloudbees.plugins.credentials.CredentialsNameProvider;
import com.cloudbees.plugins.credentials.NameWith;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Util;
import hudson.util.Secret;

/**
* Credentials that have an ID, description and EMLauncher Hostname API token pair and SSL enable flag.
*
*/
@NameWith(value = EMLauncherCredentials.NameProvider.class, priority = 32)
public interface EMLauncherCredentials extends StandardCredentials {
/**
*
*
*/
String getApiHost();
Secret getApiToken();
boolean getSslEnable();

/**
* Our name provider.
*
* @since 1.3.12
*/
public static class NameProvider extends CredentialsNameProvider<EMLauncherCredentials> {

/**
* {@inheritDoc}
*/
@NonNull
@Override
public String getName(@NonNull EMLauncherCredentials c) {
String description = Util.fixEmptyAndTrim(c.getDescription());
return c.getApiHost() + (description != null ? " (" + description + ")" : "");
}
}
}
126 changes: 126 additions & 0 deletions src/main/java/emlauncher/EMLauncherCredentialsBinding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, 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 emlauncher;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.credentialsbinding.BindingDescriptor;
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* @author <a href="mailto:[email protected]">Kazuhide Takahashi</a>
*/
public class EMLauncherCredentialsBinding extends MultiBinding<EMLauncherCredentials> {

public final static String DEFAULT_API_HOST_VARIABLE_NAME = "API_HOST";
private final static String DEFAULT_API_TOKEN_VARIABLE_NAME = "API_TOKEN";
private final static String DEFAULT_SSL_ENABLE_VARIABLE_NAME = "SSL_ENABLE";

@NonNull
private final String apiHostVariable;
@NonNull
private final String apiTokenVariable;
@NonNull
private final String sslEnableVariable;

/**
*
* @param apiHostVariable if {@code null}, {@value DEFAULT_API_HOST_VARIABLE_NAME} will be used.
* @param apiTokenVariable if {@code null}, {@value DEFAULT_API_TOKEN_VARIABLE_NAME} will be used.
* @param sslEnableVariable if {@code null}, {@value DEFAULT_SSL_ENABLE_VARIABLE_NAME} will be used.
* @param credentialsId identifier which should be referenced when accessing the credentials from a job/pipeline.
*/
@DataBoundConstructor
public EMLauncherCredentialsBinding(@Nullable String apiHostVariable, @Nullable String apiTokenVariable, @Nullable String sslEnableVariable, String credentialsId) {
super(credentialsId);
this.apiHostVariable = StringUtils.defaultIfBlank(apiHostVariable, DEFAULT_API_HOST_VARIABLE_NAME);
this.apiTokenVariable = StringUtils.defaultIfBlank(apiTokenVariable, DEFAULT_API_TOKEN_VARIABLE_NAME);
this.sslEnableVariable = StringUtils.defaultIfBlank(sslEnableVariable, DEFAULT_SSL_ENABLE_VARIABLE_NAME);
}

@NonNull
public String getApiHostVariable() {
return apiHostVariable;
}

@NonNull
public String getApiTokenVariable() {
return apiTokenVariable;
}

@NonNull
public String getSslEnableVariable() {
return sslEnableVariable;
}

@Override
protected Class<EMLauncherCredentials> type() {
return EMLauncherCredentials.class;
}

@Override
public MultiEnvironment bind(@Nonnull Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
EMLauncherCredentials credential = getCredentials(build);
Map<String,String> m = new HashMap<String,String>();
m.put(apiHostVariable, credential.getApiHost());
m.put(apiTokenVariable, credential.getApiToken().getPlainText());
m.put(sslEnableVariable, credential.getSslEnable() ? "true": "false");
return new MultiEnvironment(m);
}

@Override
public Set<String> variables() {
return new HashSet<String>(Arrays.asList(apiHostVariable, apiTokenVariable, sslEnableVariable));
}

@Extension
public static class DescriptorImpl extends BindingDescriptor<EMLauncherCredentials> {

@Override protected Class<EMLauncherCredentials> type() {
return EMLauncherCredentials.class;
}

@Override public String getDisplayName() {
return Messages.EMLauncherCredentials_DisplayName();
}
}

}
43 changes: 43 additions & 0 deletions src/main/java/emlauncher/EMLauncherCredentialsHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package emlauncher;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import hudson.model.ItemGroup;
import hudson.security.ACL;
import hudson.util.ListBoxModel;
import org.apache.commons.lang.StringUtils;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.util.Collections;

/**
* @author <a href="mailto:[email protected]">Kazuhide Takahashi</a>
*/
public class EMLauncherCredentialsHelper {

private EMLauncherCredentialsHelper() {}

@CheckForNull
public static EMLauncherCredentials getCredentials(@Nullable String credentialsId, ItemGroup context) {
if (StringUtils.isBlank(credentialsId)) {
return null;
}
return (EMLauncherCredentials) CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(EMLauncherCredentials.class, context,
ACL.SYSTEM, Collections.EMPTY_LIST),
CredentialsMatchers.withId(credentialsId));
}

public static ListBoxModel doFillCredentialsIdItems(ItemGroup context) {
return new StandardListBoxModel()
.withEmptySelection()
.withMatching(
CredentialsMatchers.always(),
CredentialsProvider.lookupCredentials(EMLauncherCredentials.class,
context,
ACL.SYSTEM,
Collections.EMPTY_LIST));
}
}
Loading