Ejemplo n.º 1
0
// buildDPNBag bags the DPNBag object that will eventually be entered into
// the DPN registry. This is not the bag itself, just the DPN registry entry.
func (packager *DPNPackager) buildDPNBag(manifest *models.DPNIngestManifest) {
	packager.Context.MessageLog.Info("Building DPN bag record for %s",
		manifest.IntellectualObject.Identifier)
	depositingInstitution := packager.getInstitution(manifest)
	if depositingInstitution == nil {
		return
	}
	manifest.DPNBag = models.NewDPNBag(
		manifest.IntellectualObject.Identifier,
		depositingInstitution.DPNUUID,
		packager.Context.Config.DPN.LocalNode)

	// Calculate the sha256 digest of the tag manifest. This is used for
	// validating bag transfers in DPN. Note that we are NOT using a
	// nonce when we call shaHash.Sum(nil). Though the DPN spec allows
	// us to use a nonce, no nodes are using nonces as of late 2016.
	tagManifestFile := filepath.Join(manifest.LocalDir, "tagmanifest-sha256.txt")
	if !fileutil.FileExists(tagManifestFile) {
		manifest.PackageSummary.AddError("Cannot find tag manifest %s", tagManifestFile)
		return
	}
	reader, err := os.Open(tagManifestFile)
	if err != nil {
		manifest.PackageSummary.AddError("Cannot read tag manifest at %s: %v",
			tagManifestFile, err)
		return
	}
	defer reader.Close()
	shaHash := sha256.New()
	io.Copy(shaHash, reader)
	tagManifestDigest := fmt.Sprintf("%x", shaHash.Sum(nil))

	// Now create the MessageDigest for this bag, with the tag manifest
	// checksum that will be used to verify transfers. When a remote
	// node copies this bag to fulfill a replication request, we expect
	// the node to return this fixity value as proof that it received
	// a valid copy of the bag.
	digest := &models.MessageDigest{
		Bag:       manifest.DPNBag.UUID,
		Algorithm: constants.AlgSha256,
		Node:      packager.Context.Config.DPN.LocalNode,
		Value:     tagManifestDigest,
		CreatedAt: time.Now().UTC(),
	}
	manifest.DPNBag.MessageDigests = append(manifest.DPNBag.MessageDigests, digest)

	// Now that we have a valid DPN bag object, we can name the tar file.
	// According to the DPN spec, the tar file name should be the bag's
	// UUID plus a ".tar" extension.
	parentOfBagDir := filepath.Dir(manifest.LocalDir)
	manifest.LocalTarFile = filepath.Join(parentOfBagDir, manifest.DPNBag.UUID+".tar")
}
Ejemplo n.º 2
0
func TestNewDPNBag(t *testing.T) {
	bag := models.NewDPNBag("local_id", "some_member", "some_node")
	assert.NotNil(t, bag)
	assert.NotEmpty(t, bag.UUID)
	assert.Equal(t, bag.UUID, bag.FirstVersionUUID)
	assert.Equal(t, "local_id", bag.LocalId)
	assert.Equal(t, "some_member", bag.Member)
	assert.Equal(t, "some_node", bag.AdminNode)
	assert.Equal(t, "some_node", bag.IngestNode)
	assert.EqualValues(t, 1, bag.Version)
	assert.Equal(t, "D", bag.BagType)
	assert.NotNil(t, bag.MessageDigests)
	assert.Empty(t, bag.MessageDigests)
	assert.NotNil(t, bag.Interpretive)
	assert.Empty(t, bag.Interpretive)
	assert.NotNil(t, bag.Rights)
	assert.Empty(t, bag.Rights)
	assert.NotNil(t, bag.ReplicatingNodes)
	assert.Empty(t, bag.ReplicatingNodes)
	assert.False(t, bag.CreatedAt.IsZero())
	assert.False(t, bag.UpdatedAt.IsZero())
}