Skip to content

Commit

Permalink
TrustDB: Make it possible to use transactions (#2251)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukedirtwalker authored Dec 18, 2018
1 parent 1f66006 commit 96a0239
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 212 deletions.
53 changes: 43 additions & 10 deletions go/lib/infra/modules/trust/trustdb/trustdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,85 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package trustdb provides wrappers for SQL calls for managing a database
// containing TRCs and Certificate Chains.
package trustdb

import (
"context"
"database/sql"
"io"

"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/scrypto/cert"
"github.com/scionproto/scion/go/lib/scrypto/trc"
)

// TrustDB is a database containing Certificates, Chains and TRCs, stored in JSON format.
// TrustDB is the interface that all trust databases have to implement.
// Read and Write interactions with this interface have to happen in individual transactions
// (either explicit or implicit).
type TrustDB interface {
Read
Write
BeginTransaction(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
io.Closer
}

// Read contains all read operation of the trust DB.
// On errors, GetXxx methods return nil and the error. If no error occurred,
// but the database query yielded 0 results, the first returned value is nil.
type Read interface {
// GetIssCertVersion returns the specified version of the issuer certificate for
// ia. If version is scrypto.LatestVer, this is equivalent to GetIssCertMaxVersion.
GetIssCertVersion(ctx context.Context, ia addr.IA, version uint64) (*cert.Certificate, error)
// GetIssCertMaxVersion returns the max version of the issuer certificate for ia.
GetIssCertMaxVersion(ctx context.Context, ia addr.IA) (*cert.Certificate, error)
// InsertIssCert inserts the issuer certificate.
InsertIssCert(ctx context.Context, crt *cert.Certificate) (int64, error)
// GetLeafCertVersion returns the specified version of the leaf certificate for
// ia. If version is scrypto.LatestVer, this is equivalent to GetLeafCertMaxVersion.
GetLeafCertVersion(ctx context.Context, ia addr.IA, version uint64) (*cert.Certificate, error)
// GetLeafCertMaxVersion returns the max version of the leaf certificate for ia.
GetLeafCertMaxVersion(ctx context.Context, ia addr.IA) (*cert.Certificate, error)
// InsertLeafCert inserts the leaf certificate.
InsertLeafCert(ctx context.Context, crt *cert.Certificate) (int64, error)
// GetChainVersion returns the specified version of the certificate chain for
// ia. If version is scrypto.LatestVer, this is equivalent to GetChainMaxVersion.
GetChainVersion(ctx context.Context, ia addr.IA, version uint64) (*cert.Chain, error)
// GetChainMaxVersion returns the max version of the chain for ia.
GetChainMaxVersion(ctx context.Context, ia addr.IA) (*cert.Chain, error)
// GetAllChains returns all chains in the database.
GetAllChains(ctx context.Context) ([]*cert.Chain, error)
// InsertChain inserts chain into the database. The first return value is the
// number of rows affected.
InsertChain(ctx context.Context, chain *cert.Chain) (int64, error)
// GetTRCVersion returns the specified version of the TRC for
// isd. If version is scrypto.LatestVer, this is equivalent to GetTRCMaxVersion.
GetTRCVersion(ctx context.Context, isd addr.ISD, version uint64) (*trc.TRC, error)
// GetTRCMaxVersion returns the max version of the TRC for ia.
GetTRCMaxVersion(ctx context.Context, isd addr.ISD) (*trc.TRC, error)
// GetAllTRCs fetches all TRCs from the database.
GetAllTRCs(ctx context.Context) ([]*trc.TRC, error)
}

// Write contains all write operations fo the trust DB.
type Write interface {
// InsertIssCert inserts the issuer certificate.
InsertIssCert(ctx context.Context, crt *cert.Certificate) (int64, error)
// InsertLeafCert inserts the leaf certificate.
InsertLeafCert(ctx context.Context, crt *cert.Certificate) (int64, error)
// InsertChain inserts chain into the database. The first return value is the
// number of rows affected.
InsertChain(ctx context.Context, chain *cert.Chain) (int64, error)
// InsertTRC inserts trcobj into the database. The first return value is the
// number of rows affected.
InsertTRC(ctx context.Context, trcobj *trc.TRC) (int64, error)
// GetAllTRCs fetches all TRCs from the database.
GetAllTRCs(ctx context.Context) ([]*trc.TRC, error)
io.Closer
}

// Transaction represents a trust DB transaction with an ongoing transaction.
// To end the transaction either Rollback or Commit should be called. Calling Commit or Rollback
// multiple times will result in an error.
type Transaction interface {
Read
Write
// Commit commits the transaction.
// Returns the underlying TrustDB connection.
Commit() error
// Rollback rollbacks the transaction.
// Returns the underlying TrustDB connection.
Rollback() error
}
Loading

0 comments on commit 96a0239

Please sign in to comment.