use of com.bluenimble.platform.icli.mgm.Keys in project serverless by bluenimble.
the class KeysCommand method execute.
@Override
public CommandResult execute(Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
String cmd = (String) tool.currentContext().get(ToolContext.CommandLine);
try {
if ("reload".equals(cmd)) {
BlueNimble.loadKeys(tool);
}
Map<String, Keys> secrets = BlueNimble.allKeys();
if (secrets == null || secrets.isEmpty()) {
tool.printer().info("No keys found!");
return null;
}
Iterator<String> secIter = secrets.keySet().iterator();
while (secIter.hasNext()) {
String sname = secIter.next();
Keys s = secrets.get(sname);
boolean current = BlueNimble.keys() != null && sname.equals(BlueNimble.keys().alias());
tool.printer().content(// __PS__YELLOW:(C) _|_keyAlias
current ? PrintSpec.Start + Colors.Yellow + PrintSpec.TextSep + "(C) " + PrintSpec.Split + sname : sname, (Lang.isNullOrEmpty(s.name()) ? Lang.BLANK : " Name | " + s.name() + Lang.ENDLN) + (Lang.isNullOrEmpty(s.domain()) ? Lang.BLANK : " Endpoint | " + s.domain() + Lang.ENDLN) + (Lang.isNullOrEmpty(s.issuer()) ? Lang.BLANK : " Issuer | " + s.issuer() + Lang.ENDLN) + (Lang.isNullOrEmpty(s.whenIssued()) ? Lang.BLANK : " Issued | " + s.whenIssued() + Lang.ENDLN) + (Lang.isNullOrEmpty(s.expiresOn()) ? Lang.BLANK : " Expires | " + s.expiresOn() + Lang.ENDLN) + (Lang.isNullOrEmpty(s.stamp()) ? Lang.BLANK : " Stamp | " + s.stamp() + Lang.ENDLN) + "Access Key | " + s.accessKey());
}
} catch (Exception ex) {
throw new CommandExecutionException(ex.getMessage(), ex);
}
return new DefaultCommandResult(CommandResult.OK, null);
}
use of com.bluenimble.platform.icli.mgm.Keys in project serverless by bluenimble.
the class RemoteUtils method processRequest.
public static CommandResult processRequest(Tool tool, JsonObject source, final Map<String, String> options) throws CommandExecutionException {
if (Lang.isDebugMode()) {
tool.printer().content("Remote Command", source.toString());
}
JsonObject oKeys = null;
Keys keys = BlueNimble.keys();
if (keys != null) {
oKeys = keys.json();
} else {
oKeys = new JsonObject();
}
@SuppressWarnings("unchecked") Map<String, Object> vars = (Map<String, Object>) tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS);
Object oTrustAll = vars.get(DefaultVars.SslTrust);
if (oTrustAll == null) {
Http.setTrustAll(false);
} else {
Http.setTrustAll(Lang.TrueValues.contains(String.valueOf(oTrustAll)));
}
boolean isOutFile = AbstractTool.CMD_OUT_FILE.equals(vars.get(AbstractTool.CMD_OUT));
List<Object> streams = new ArrayList<Object>();
HttpResponse response = null;
try {
HttpRequest request = request(oKeys, Json.getObject(source, Spec.request.class.getSimpleName()), tool, BlueNimble.Config, options, streams);
response = Http.send(request);
String contentType = response.getContentType();
if (contentType == null) {
contentType = ApiContentTypes.Text;
}
int indexOfSemi = contentType.indexOf(Lang.SEMICOLON);
if (indexOfSemi > 0) {
contentType = contentType.substring(0, indexOfSemi).trim();
}
OutputStream out = System.out;
if (Printable.contains(contentType) && !isOutFile) {
out = new ByteArrayOutputStream();
response.getBody().dump(out, Encodings.UTF8, null);
}
List<HttpHeader> rHeaders = response.getHeaders();
if (rHeaders != null && !rHeaders.isEmpty()) {
JsonObject oHeaders = new JsonObject();
for (HttpHeader h : rHeaders) {
oHeaders.set(h.getName(), Lang.join(h.getValues(), Lang.COMMA));
}
vars.put(RemoteResponseHeaders, oHeaders);
}
if (contentType.startsWith(ApiContentTypes.Json)) {
JsonObject result = new JsonObject(new String(((ByteArrayOutputStream) out).toByteArray()));
String trace = null;
if (response.getStatus() >= 400) {
trace = result.getString("trace");
result.remove("trace");
}
if (trace != null && Lang.isDebugMode()) {
vars.put(RemoteResponseError, trace);
}
if (response.getStatus() < 400) {
return new DefaultCommandResult(CommandResult.OK, result);
} else {
return new DefaultCommandResult(CommandResult.KO, result);
}
} else if (contentType.startsWith(ApiContentTypes.Yaml)) {
Yaml yaml = new Yaml();
String ys = new String(((ByteArrayOutputStream) out).toByteArray());
ys = Lang.replace(ys, Lang.TAB, " ");
@SuppressWarnings("unchecked") Map<String, Object> map = yaml.loadAs(ys, Map.class);
Object trace = null;
if (response.getStatus() >= 400) {
trace = map.get("trace");
map.remove("trace");
}
if (trace != null && Lang.isDebugMode()) {
vars.put(RemoteResponseError, trace);
}
if (response.getStatus() < 400) {
return new DefaultCommandResult(CommandResult.OK, new YamlObject(map));
} else {
return new DefaultCommandResult(CommandResult.KO, new YamlObject(map));
}
} else if (contentType.startsWith(ApiContentTypes.Text) || contentType.startsWith(ApiContentTypes.Html)) {
String content = new String(((ByteArrayOutputStream) out).toByteArray());
if (response.getStatus() < 400) {
return new DefaultCommandResult(CommandResult.OK, content);
} else {
return new DefaultCommandResult(CommandResult.KO, content);
}
} else {
if (response.getStatus() < 400) {
if (isOutFile) {
return new DefaultCommandResult(CommandResult.OK, response.getBody().get(0).toInputStream());
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
response.getBody().dump(baos, Encodings.UTF8, null);
return new DefaultCommandResult(CommandResult.OK, new String(((ByteArrayOutputStream) baos).toByteArray()));
}
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
response.getBody().dump(baos, Encodings.UTF8, null);
return new DefaultCommandResult(CommandResult.KO, new String(((ByteArrayOutputStream) baos).toByteArray()));
}
}
} catch (Exception e) {
throw new CommandExecutionException(e.getMessage(), e);
} finally {
if (streams != null) {
for (Object s : streams) {
if (s instanceof InputStream) {
IOUtils.closeQuietly((InputStream) s);
} else if (s instanceof StreamPointer) {
StreamPointer sp = (StreamPointer) s;
IOUtils.closeQuietly(sp.getStream());
if (sp.shouldDelete()) {
try {
FileUtils.delete(sp.getPointer());
} catch (IOException e) {
// IGNORE
}
}
}
}
}
}
}
use of com.bluenimble.platform.icli.mgm.Keys in project serverless by bluenimble.
the class MacroSourceCommand method execute.
@Override
public CommandResult execute(final Tool tool, Map<String, CommandOption> options) throws CommandExecutionException {
InputStream input = null;
SimpleBindings bindings = new SimpleBindings();
String command = (String) tool.currentContext().get(ToolContext.CommandLine);
if (!Lang.isNullOrEmpty(command)) {
bindings.put("Command", command);
}
bindings.put("Home", BlueNimble.Work);
bindings.put("Config", BlueNimble.Config);
bindings.put("Tool", new JsTool(tool));
bindings.put(JavaClass, new Function<String, Class<?>>() {
@Override
public Class<?> apply(String type) {
try {
return MacroSourceCommand.class.getClassLoader().loadClass(type);
} catch (ClassNotFoundException cnfe) {
throw new RuntimeException(cnfe);
}
}
});
bindings.put("Vars", tool.getContext(Tool.ROOT_CTX).get(ToolContext.VARS));
Keys keys = BlueNimble.keys();
if (keys != null) {
bindings.put("Keys", keys.json());
}
try {
input = new FileInputStream(script);
List<InputStream> blocks = new ArrayList<InputStream>();
blocks.add(new ByteArrayInputStream(Native.getBytes()));
blocks.add(input);
Engine.eval(new InputStreamReader(new SequenceInputStream(Collections.enumeration(blocks))), bindings);
} catch (Exception e) {
throw new CommandExecutionException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(input);
}
return null;
}
Aggregations