Skip to content

Commit

Permalink
Merge pull request #291 from uptane/feat/cli-sign-json-blog
Browse files Browse the repository at this point in the history
New cli command sign-json
  • Loading branch information
simao authored Mar 24, 2022
2 parents 33533b3 + 4b1de9a commit e441092
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
17 changes: 17 additions & 0 deletions cli/src/main/scala/com/advancedtelematic/tuf/cli/Cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ case class Config(command: Command,
serverCertPath: Option[Path] = None,
delegatedPaths: List[DelegatedPathPattern] = List.empty,
keyPaths: List[Path] = List.empty,
pubKeyPath: Option[Path] = None,
force: Boolean = false,
reposerverUrl: Option[URI] = None,
verbose: Boolean = false,
Expand Down Expand Up @@ -165,6 +166,22 @@ object Cli extends App with VersionInfo {

note(" " + sys.props("line.separator"))

cmd("sign-json")
.toCommand(SignUserJson)
.text("Signs valid user provided json with a specified key")
.children(
opt[Path]("priv-key").abbr("k")
.text("The path to the private key to use to sign json")
.action { (arg, c) => c.copy(keyPaths = arg :: c.keyPaths) },
opt[Path]("pub-key").abbr("p")
.text("The path to the public key to use to sign json")
.toConfigOptionParam('pubKeyPath),
opt[Path]('i', "input").toConfigOptionParam('inputPath)
.text("path to input json")
)

note(" " + sys.props("line.separator"))

cmd("user-keys").children(
keysPathOpt(this),
cmd("gen")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import java.net.URI
import java.time.{Instant, Period, ZoneOffset}
import java.util.concurrent.TimeUnit
import com.advancedtelematic.libats.data.DataType.Checksum
import com.advancedtelematic.libtuf.crypt.Sha256FileDigest
import com.advancedtelematic.libtuf.crypt.{Sha256FileDigest, TufCrypto}
import com.advancedtelematic.libtuf.data.ClientCodecs._
import com.advancedtelematic.libtuf.data.ClientDataType.{ClientTargetItem, RootRole, TargetCustom}
import com.advancedtelematic.libtuf.data.TufCodecs._
import com.advancedtelematic.libtuf.data.TufDataType.TargetFormat.TargetFormat
import com.advancedtelematic.libtuf.data.TufDataType.{HardwareIdentifier, RoleType, TargetFilename, TargetFormat, TargetName, TargetVersion, ValidTargetFilename}
import com.advancedtelematic.libtuf.data.TufDataType.{HardwareIdentifier, RoleType, SignedPayload, TargetFilename, TargetFormat, TargetName, TargetVersion, ValidTargetFilename}
import com.advancedtelematic.libtuf.http.{ReposerverClient, TufServerClient}
import com.advancedtelematic.tuf.cli.CliConfigOptionOps._
import com.advancedtelematic.tuf.cli.Commands._
Expand Down Expand Up @@ -221,6 +221,18 @@ object CommandHandler {
userKeyStorage.genKeys(keyName, config.keyType, config.keySize)
}.sequence_

case SignUserJson =>
for {
inJson <- io.circe.jawn.parsePath(config.inputPath.valueOrConfigError).toTry
pubKey <- CliKeyStorage.readPublicKey(config.pubKeyPath.valueOrConfigError)
privKey <- CliKeyStorage.readPrivateKey(config.keyPaths.headOption.valueOrConfigError)
} yield {
val signature = TufCrypto.signPayload(privKey, inJson).toClient(pubKey.id)
val payload = SignedPayload(Seq(signature), inJson, inJson)

config.outputPath.streamOrStdout.write(payload.asJson.spaces2.getBytes)
}

case IdUserKey =>
CliKeyStorage.readPublicKey(config.inputPath.valueOrConfigError).map { key =>
config.outputPath.streamOrStdout.write(key.id.value.getBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ object Commands {
case object CreateDelegation extends Command
case object SignDelegation extends Command
case object GenUserKey extends Command
case object SignUserJson extends Command
case object IdUserKey extends Command
case object PushDelegation extends Command
case object PullDelegation extends Command
Expand Down

0 comments on commit e441092

Please sign in to comment.