From a4b285ae98b8cbe59771ec4f81ed2a30e51c58ed Mon Sep 17 00:00:00 2001 From: Brian Hendriks Date: Wed, 28 Aug 2024 15:08:22 -0700 Subject: [PATCH] signature in log (should be optional) --- go/cmd/dolt/commands/utils.go | 4 +-- .../doltcore/sqle/dolt_log_table_function.go | 4 ++- .../doltcore/sqle/dprocedures/dolt_commit.go | 27 ++----------------- go/libraries/utils/gpg/sign.go | 11 +++----- 4 files changed, 10 insertions(+), 36 deletions(-) diff --git a/go/cmd/dolt/commands/utils.go b/go/cmd/dolt/commands/utils.go index 391dd87565..34bb4b8d4d 100644 --- a/go/cmd/dolt/commands/utils.go +++ b/go/cmd/dolt/commands/utils.go @@ -676,8 +676,8 @@ func getCommitInfo(queryist cli.Queryist, sqlCtx *sql.Context, ref string) (*Com isHead := commitHash == hashOfHead var signature string - if len(row) > 6 { - signature = row[6].(string) + if len(row) > 7 { + signature = row[7].(string) } localBranchesForHash, err := getBranchesForHash(queryist, sqlCtx, commitHash, true) diff --git a/go/libraries/doltcore/sqle/dolt_log_table_function.go b/go/libraries/doltcore/sqle/dolt_log_table_function.go index d7853d1503..31ba438201 100644 --- a/go/libraries/doltcore/sqle/dolt_log_table_function.go +++ b/go/libraries/doltcore/sqle/dolt_log_table_function.go @@ -749,7 +749,7 @@ func (itr *logTableFunctionRowIter) Next(ctx *sql.Context) (sql.Row, error) { return nil, err } - row := sql.NewRow(commitHash.String(), meta.Name, meta.Email, meta.Time(), meta.Description, meta.Signature) + row := sql.NewRow(commitHash.String(), meta.Name, meta.Email, meta.Time(), meta.Description) if itr.showParents { prStr, err := getParentsString(ctx, commit) @@ -765,6 +765,8 @@ func (itr *logTableFunctionRowIter) Next(ctx *sql.Context) (sql.Row, error) { row = row.Append(sql.NewRow(getRefsString(branchNames, isHead))) } + row = row.Append(sql.NewRow(meta.Signature)) + return row, nil } diff --git a/go/libraries/doltcore/sqle/dprocedures/dolt_commit.go b/go/libraries/doltcore/sqle/dprocedures/dolt_commit.go index aac862c030..8faa4201ad 100644 --- a/go/libraries/doltcore/sqle/dprocedures/dolt_commit.go +++ b/go/libraries/doltcore/sqle/dprocedures/dolt_commit.go @@ -15,7 +15,6 @@ package dprocedures import ( - "context" "errors" "fmt" "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" @@ -201,13 +200,12 @@ func doDoltCommit(ctx *sql.Context, args []string) (string, bool, error) { return "", false, err } - signed, signature, err := gpgSign(ctx, keyId, []byte(strToSign)) + signature, err := gpg.Sign(ctx, keyId, []byte(strToSign)) if err != nil { return "", false, err } - fmt.Println(signed, signature) - pendingCommit.CommitOptions.Meta.Signature = signature + pendingCommit.CommitOptions.Meta.Signature = string(signature) } newCommit, err := dSess.DoltCommit(ctx, dbName, dSess.GetTransaction(), pendingCommit) @@ -267,24 +265,3 @@ func commitSignatureStr(ctx *sql.Context, dbName string, roots doltdb.Roots, csp return strings.Join(lines, "\n"), nil } - -func gpgSign(ctx context.Context, keyId string, toSign []byte) (string, string, error) { - blocks, err := gpg.Sign(ctx, keyId, toSign) - if err != nil { - return "", "", fmt.Errorf("failed to sign: %w", err) - } - - signedDataBlocks := gpg.GetBlocksOfType(blocks, "PGP SIGNED MESSAGE") - - if len(signedDataBlocks) != 1 { - return "", "", fmt.Errorf("expected 1 'PGP SIGNED MESSAGE' block, got %d", len(signedDataBlocks)) - } - - signatureBlocks := gpg.GetBlocksOfType(blocks, "PGP SIGNATURE") - - if len(signatureBlocks) != 1 { - return "", "", fmt.Errorf("expected 1 PGP SIGNATURE block, got %d", len(signatureBlocks)) - } - - return string(signedDataBlocks[0].Bytes), string(signatureBlocks[0].Bytes), nil -} diff --git a/go/libraries/utils/gpg/sign.go b/go/libraries/utils/gpg/sign.go index 2205b4c8cc..a26323819b 100644 --- a/go/libraries/utils/gpg/sign.go +++ b/go/libraries/utils/gpg/sign.go @@ -11,7 +11,7 @@ import ( "strings" ) -func Sign(ctx context.Context, keyId string, message []byte) ([]*pem.Block, error) { +func Sign(ctx context.Context, keyId string, message []byte) ([]byte, error) { args := []string{"--clear-sign", "-u", keyId} cmdStr := fmt.Sprintf("gpg %s", strings.Join(args, " ")) cmd := exec.CommandContext(ctx, "gpg", args...) @@ -72,12 +72,7 @@ func Sign(ctx context.Context, keyId string, message []byte) ([]*pem.Block, erro return nil, fmt.Errorf("failed to read output for command '%s': %w", cmdStr, err) } - pemBlocks, err := decodeAllPEMBlocks(outBuf.Bytes()) - if err != nil { - return nil, fmt.Errorf("failed to decode PEM blocks: %w", err) - } - - return pemBlocks, nil + return outBuf.Bytes(), nil } func listenToOut(ctx context.Context, eg *errgroup.Group, r io.Reader) *bytes.Buffer { @@ -90,7 +85,7 @@ func listenToOut(ctx context.Context, eg *errgroup.Group, r io.Reader) *bytes.Bu } // Throws away all intersperesed text and returns all decoded PEM blocks, in the order they are read. -func decodeAllPEMBlocks(bs []byte) ([]*pem.Block, error) { +func DecodeAllPEMBlocks(bs []byte) ([]*pem.Block, error) { const beginHeaderPrefix = "BEGIN " const pemSeperator = "-----"