|
| 1 | +/* |
| 2 | + * mxisd - Matrix Identity Server Daemon |
| 3 | + * Copyright (C) 2018 Kamax Sarl |
| 4 | + * |
| 5 | + * https://www.kamax.io/ |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Affero General Public License as |
| 9 | + * published by the Free Software Foundation, either version 3 of the |
| 10 | + * License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package io.kamax.mxisd.backend.exec; |
| 22 | + |
| 23 | +import com.google.gson.JsonObject; |
| 24 | +import com.google.gson.JsonPrimitive; |
| 25 | +import io.kamax.matrix._MatrixID; |
| 26 | +import io.kamax.matrix.json.GsonUtil; |
| 27 | +import io.kamax.mxisd.UserID; |
| 28 | +import io.kamax.mxisd.UserIdType; |
| 29 | +import io.kamax.mxisd.auth.provider.AuthenticatorProvider; |
| 30 | +import io.kamax.mxisd.config.ExecConfig; |
| 31 | +import io.kamax.mxisd.exception.InternalServerError; |
| 32 | +import org.apache.commons.io.IOUtils; |
| 33 | +import org.apache.commons.lang3.StringUtils; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | +import org.springframework.beans.factory.annotation.Autowired; |
| 37 | +import org.springframework.stereotype.Component; |
| 38 | +import org.zeroturnaround.exec.ProcessExecutor; |
| 39 | +import org.zeroturnaround.exec.ProcessResult; |
| 40 | + |
| 41 | +import java.io.IOException; |
| 42 | +import java.nio.charset.StandardCharsets; |
| 43 | +import java.util.*; |
| 44 | +import java.util.concurrent.TimeoutException; |
| 45 | +import java.util.stream.Collectors; |
| 46 | + |
| 47 | +@Component |
| 48 | +public class ExecAuthStore extends ExecStore implements AuthenticatorProvider { |
| 49 | + |
| 50 | + private final transient Logger log = LoggerFactory.getLogger(ExecAuthStore.class); |
| 51 | + |
| 52 | + private ExecConfig.Auth cfg; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + public ExecAuthStore(ExecConfig cfg) { |
| 56 | + this.cfg = Objects.requireNonNull(cfg.getAuth()); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean isEnabled() { |
| 61 | + return cfg.isEnabled(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public ExecAuthResult authenticate(_MatrixID uId, String password) { |
| 66 | + Objects.requireNonNull(uId); |
| 67 | + Objects.requireNonNull(password); |
| 68 | + |
| 69 | + log.info("Performing authentication for {}", uId.getId()); |
| 70 | + |
| 71 | + ExecAuthResult result = new ExecAuthResult(); |
| 72 | + result.setId(new UserID(UserIdType.Localpart, uId.getLocalPart())); |
| 73 | + |
| 74 | + ProcessExecutor psExec = new ProcessExecutor().readOutput(true); |
| 75 | + |
| 76 | + List<String> args = new ArrayList<>(); |
| 77 | + args.add(cfg.getCommand()); |
| 78 | + args.addAll(cfg.getArgs().stream().map(arg -> arg |
| 79 | + .replace(cfg.getToken().getLocalpart(), uId.getLocalPart()) |
| 80 | + .replace(cfg.getToken().getDomain(), uId.getDomain()) |
| 81 | + .replace(cfg.getToken().getMxid(), uId.getId()) |
| 82 | + .replace(cfg.getToken().getPassword(), password) |
| 83 | + ).collect(Collectors.toList())); |
| 84 | + psExec.command(args); |
| 85 | + |
| 86 | + psExec.environment(new HashMap<>(cfg.getEnv()).entrySet().stream().peek(e -> { |
| 87 | + e.setValue(e.getValue().replace(cfg.getToken().getLocalpart(), uId.getLocalPart())); |
| 88 | + e.setValue(e.getValue().replace(cfg.getToken().getDomain(), uId.getDomain())); |
| 89 | + e.setValue(e.getValue().replace(cfg.getToken().getMxid(), uId.getId())); |
| 90 | + e.setValue(e.getValue().replace(cfg.getToken().getPassword(), password)); |
| 91 | + }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); |
| 92 | + |
| 93 | + if (StringUtils.isNotBlank(cfg.getInput())) { |
| 94 | + if (StringUtils.equals("json", cfg.getInput())) { |
| 95 | + JsonObject input = new JsonObject(); |
| 96 | + input.addProperty("localpart", uId.getLocalPart()); |
| 97 | + input.addProperty("mxid", uId.getId()); |
| 98 | + input.addProperty("password", password); |
| 99 | + psExec.redirectInput(IOUtils.toInputStream(GsonUtil.get().toJson(input), StandardCharsets.UTF_8)); |
| 100 | + } else { |
| 101 | + throw new InternalServerError(cfg.getInput() + " is not a valid executable input format"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + try { |
| 106 | + log.info("Executing {}", cfg.getCommand()); |
| 107 | + ProcessResult psResult = psExec.execute(); |
| 108 | + result.setExitStatus(psResult.getExitValue()); |
| 109 | + String output = psResult.outputUTF8(); |
| 110 | + |
| 111 | + log.info("Exit status: {}", result.getExitStatus()); |
| 112 | + if (cfg.getExit().getSuccess().contains(result.getExitStatus())) { |
| 113 | + result.setSuccess(true); |
| 114 | + if (result.isSuccess()) { |
| 115 | + if (StringUtils.equals("json", cfg.getOutput())) { |
| 116 | + JsonObject data = GsonUtil.parseObj(output); |
| 117 | + GsonUtil.findPrimitive(data, "success") |
| 118 | + .map(JsonPrimitive::getAsBoolean) |
| 119 | + .ifPresent(result::setSuccess); |
| 120 | + GsonUtil.findObj(data, "profile") |
| 121 | + .flatMap(p -> GsonUtil.findString(p, "display_name")) |
| 122 | + .ifPresent(v -> result.getProfile().setDisplayName(v)); |
| 123 | + } else { |
| 124 | + log.debug("Command output:{}{}", "\n", output); |
| 125 | + } |
| 126 | + } |
| 127 | + } else if (cfg.getExit().getFailure().contains(result.getExitStatus())) { |
| 128 | + log.debug("{} stdout:{}{}", cfg.getCommand(), "\n", output); |
| 129 | + result.setSuccess(false); |
| 130 | + } else { |
| 131 | + log.error("{} stdout:{}{}", cfg.getCommand(), "\n", output); |
| 132 | + throw new InternalServerError("Exec auth command returned with unexpected exit status"); |
| 133 | + } |
| 134 | + |
| 135 | + return result; |
| 136 | + } catch (IOException | InterruptedException | TimeoutException e) { |
| 137 | + throw new InternalServerError(e); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments