Where to put .p12 file for a Stash plugin?

125 Views Asked by At

So I'm trying to write a Stash post receive hook plugin that eventually writes some pertinent information to a Google spreadsheet. To do this, I have to pull the private key for Oauth2 authentication from a .p12 file. I made a couple test projects that were just plain Java command line apps and I would just out the .p12 file in the root and call it from there. I made another one where I would call it from the resources folder and that worked fine too.

I ported that code over, got all my dependencies settled up, and ran it. I naively dropped the .p12 file in the root and obviously got a file not found exception. Looked at the target directory, saw it never brought that over, readjusted and put it in the resources folder to see if that would get carried over. Another file not found exception and still not in the target directory. I tried putting it in a couple different other places just to see if it would be brought over. Nothing so far has worked.

What am I doing wrong here?

I guess I could put the file right on my Stash instance and call it from there, but I would rather have it self contained right in the plugin.

I've searched around a lot but didn't find anything really relevant. Anyone have any suggestions?

1

There are 1 best solutions below

0
On BEST ANSWER

If anyone else sees this and wonders how I solved it... An Atlassian dev pointed to me that my main issue was that I was just putting the .p12 file in the root resources directory. As soon as I dropped it into its own folder in there, it worked like a charm. My final code for getting this all to work (with some help of other Stack Overflow questions)

String sheetURL = "[email protected]";
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
String[] SCOPESArray = {"https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds"};

final List SCOPES = Arrays.asList(SCOPESArray);
GoogleCredential credential = null;
InputStream stream = null;
File tempFile = null;

try {
    stream = this.getClass().getClassLoader().getResourceAsStream("authentication/key.p12");
    tempFile = File.createTempFile("temp", "temp");
    IOUtils.copy(stream, new FileOutputStream(tempFile));
}
catch (IOException e) {
    print(e.toString());
}
finally {
    if (stream != null) {
        IOUtils.closeQuietly(stream);
    }
}

credential = new GoogleCredential.Builder()
    .setTransport(httpTransport)
    .setJsonFactory(jsonFactory)
    .setServiceAccountId(sheetURL)
    .setServiceAccountScopes(SCOPES)
    .setServiceAccountPrivateKeyFromP12File(tempFile) 
    .build();