libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

Setup.kt (4980B)


      1 /*
      2  * This file is part of LibEuFin.
      3  * Copyright (C) 2025 Taler Systems S.A.
      4 
      5  * LibEuFin is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU Affero General Public License as
      7  * published by the Free Software Foundation; either version 3, or
      8  * (at your option) any later version.
      9 
     10  * LibEuFin is distributed in the hope that it will be useful, but
     11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
     13  * Public License for more details.
     14 
     15  * You should have received a copy of the GNU Affero General Public
     16  * License along with LibEuFin; see the file COPYING.  If not, see
     17  * <http://www.gnu.org/licenses/>
     18  */
     19 
     20 package tech.libeufin.ebisync.cli
     21 
     22 import com.github.ajalt.clikt.core.CliktCommand
     23 import com.github.ajalt.clikt.core.Context
     24 import com.github.ajalt.clikt.parameters.groups.provideDelegate
     25 import com.github.ajalt.clikt.parameters.options.flag
     26 import com.github.ajalt.clikt.parameters.options.option
     27 import io.ktor.client.*
     28 import tech.libeufin.ebics.*
     29 import tech.libeufin.ebisync.*
     30 import tech.libeufin.common.*
     31 import tech.libeufin.common.crypto.CryptoUtil
     32 import java.nio.file.FileAlreadyExistsException
     33 import java.nio.file.Path
     34 import java.nio.file.StandardOpenOption
     35 import java.time.Instant
     36 import kotlin.io.path.Path
     37 import kotlin.io.path.writeBytes
     38 
     39 fun expectFullKeys(cfg: EbicsKeysConfig): Pair<ClientPrivateKeysFile, BankPublicKeysFile> =
     40     expectFullKeys(cfg, "libeufin-ebisync setup")
     41 
     42 class Setup: TalerCmd() {
     43     override fun help(context: Context) = "Set up the EBICS subscriber"
     44 
     45     private val forceKeysResubmission by option(
     46         help = "Resubmits all the keys to the bank"
     47     ).flag(default = false)
     48     private val autoAcceptKeys by option(
     49         help = "Accepts the bank keys without interactively asking the user"
     50     ).flag(default = false)
     51     private val generateRegistrationPdf by option(
     52         help = "Generates the PDF with the client public keys to send to the bank"
     53     ).flag(default = false)
     54     private val ebicsLog by ebicsLogOption()
     55     /**
     56      * This function collects the main steps of setting up an EBICS access.
     57      */
     58     override fun run() = cliCmd(logger) {
     59         val cfg = ebisyncConfig(config)
     60 
     61         val httpClient = httpClient()
     62         val ebicsLogger = EbicsLogger(ebicsLog)
     63 
     64         logger.info("Check EBICS setup")
     65 
     66         val (clientKeys, bankKeys) = ebicsSetup(
     67             httpClient,
     68             ebicsLogger,
     69             cfg,
     70             cfg,
     71             cfg.setup,
     72             forceKeysResubmission,
     73             generateRegistrationPdf,
     74             autoAcceptKeys,
     75             true
     76         )
     77         
     78         // Check account information
     79         logger.info("Doing administrative request HKD")
     80         cfg.withDb { db, _ ->
     81             EbicsClient(
     82                 cfg,
     83                 httpClient, 
     84                 db.ebics,
     85                 ebicsLogger,
     86                 clientKeys,
     87                 bankKeys
     88             ).download(EbicsOrder.V3.HKD) { stream ->
     89                 val (partner, users) = EbicsAdministrative.parseHKD(stream)
     90                 // Debug logging
     91                 logger.debug {
     92                     buildString {
     93                         if (partner.name != null || partner.accounts.isNotEmpty()) {
     94                             append("Partner Info: ")
     95                             if (partner.name != null) {
     96                                 append("'")
     97                                 append(partner.name)
     98                                 append("'")
     99                             }
    100                             for ((currency, iban, bic) in partner.accounts) {
    101                                 append(' ')
    102                                 append(currency)
    103                                 append('-')
    104                                 append(iban)
    105                                 append('-')
    106                                 append(bic)
    107                             }
    108                             append('\n')
    109                         }
    110                         append("Supported orders:\n")
    111                         for ((order, description) in partner.orders) {
    112                             append("- ")
    113                             append(order.description())
    114                             append(": ")
    115                             append(description)
    116                             append('\n')
    117                         }
    118                     }
    119                 }
    120             }
    121         }
    122         logger.info("EBICS ready")
    123 
    124         logger.info("Check fetch destination setup")
    125         when (val dest = DestinationClient.prepare(cfg.fetch.destination, httpClient)) {
    126             null -> logger.warn("No destination configured")
    127             is DestinationClient.AzureBlobStorage -> {
    128                 dest.client.getContainterMetadata(dest.container)
    129             }
    130         }
    131         logger.info("Fetch destination ready")
    132 
    133         println("setup ready")
    134     }
    135 }