Newer
Older
/*
************************************************************************
**** C A N A D I A N A S T R O N O M Y D A T A C E N T R E *****
*
* (c) 2015. (c) 2015.
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
* All rights reserved Tous droits reserves
*
* NRC disclaims any warranties Le CNRC denie toute garantie
* expressed, implied, or statu- enoncee, implicite ou legale,
* tory, of any kind with respect de quelque nature que se soit,
* to the software, including concernant le logiciel, y com-
* without limitation any war- pris sans restriction toute
* ranty of merchantability or garantie de valeur marchande
* fitness for a particular pur- ou de pertinence pour un usage
* pose. NRC shall not be liable particulier. Le CNRC ne
* in any event for any damages, pourra en aucun cas etre tenu
* whether direct or indirect, responsable de tout dommage,
* special or general, consequen- direct ou indirect, particul-
* tial or incidental, arising ier ou general, accessoire ou
* from the use of the software. fortuit, resultant de l'utili-
* sation du logiciel.
*
*
* @author adriand
*
* @version $Revision: $
*
*
**** C A N A D I A N A S T R O N O M Y D A T A C E N T R E *****
************************************************************************
*/
package ca.nrc.cadc.cert;
Patrick Dowler
committed
import ca.nrc.cadc.auth.HttpPrincipal;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.security.PrivilegedAction;
import java.security.Security;
import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import ca.nrc.cadc.util.ArgumentMap;
import ca.nrc.cadc.util.StringUtil;
Patrick Dowler
committed
import javax.security.auth.x500.X500Principal;
public abstract class AbstractCertGenAction implements PrivilegedAction<Object>
{
Patrick Dowler
committed
private static Logger LOGGER = Logger.getLogger(AbstractCertGenAction.class);
protected int expiring;
protected String userid;
protected final URI serviceID;
protected AbstractCertGenAction(URI serviceID)
{
this.serviceID = serviceID;
}
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
public boolean init(final ArgumentMap argMap) throws IOException
{
String expiringString = argMap.getValue(Main.ARG_EXPIRING);
String userIDString = argMap.getValue(Main.ARG_USERID);
if (expiringString == null && userIDString == null)
{
LOGGER.error("One of " + Main.ARG_EXPIRING + " or " +
Main.ARG_USERID + " must be provided.");
return false;
}
if (expiringString != null && userIDString != null)
{
LOGGER.error("Only one of " + Main.ARG_EXPIRING + " or " +
Main.ARG_USERID + " must be provided.");
return false;
}
if (expiringString != null)
{
expiring = parseExpire(argMap);
}
else
{
userid = userIDString;
}
if (Security.getProvider("BC") == null)
{
Security.addProvider(new BouncyCastleProvider());
}
return true;
}
Patrick Dowler
committed
abstract protected X500Principal[] getExpiring(int expire);
abstract protected X500Principal getCertificateDN(HttpPrincipal userId);
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
private int parseExpire(ArgumentMap argMap)
{
int expire = 0;
String expireStr = argMap.getValue(Main.ARG_EXPIRING);
if (StringUtil.hasText(expireStr))
{
try
{
expire = Integer.parseInt(expireStr);
}
catch (NumberFormatException e)
{
LOGGER.debug(Main.ARG_EXPIRING + " must be an integer");
LOGGER.debug("Using the default value instead");
}
}
if (expire == 0)
expire = Main.DEFAULT_EXPIRE;
return expire;
}
@Override
public String run()
{
try
{
runCommand();
return null;
}
catch (Exception e)
{
LOGGER.debug("run - ERROR \n" + e.getMessage());
throw new RuntimeException("execution error", e);
}
}
protected void msg(String msg)
{
Main.msg(msg);
}
protected abstract void runCommand() throws Exception;
}