コード例 #1
0
ファイル: mount.go プロジェクト: nawien-sharma/vault
// persistMounts is used to persist the mount table after modification
func (c *Core) persistMounts(table *MountTable) error {
	if table.Type != mountTableType {
		c.logger.Error("core: given table to persist has wrong type", "actual_type", table.Type, "expected_type", mountTableType)
		return fmt.Errorf("invalid table type given, not persisting")
	}

	for _, entry := range table.Entries {
		if entry.Table != table.Type {
			c.logger.Error("core: given entry to persist in mount table has wrong table value", "path", entry.Path, "entry_table_type", entry.Table, "actual_type", table.Type)
			return fmt.Errorf("invalid mount entry found, not persisting")
		}
	}

	// Encode the mount table into JSON and compress it (lzw).
	compressedBytes, err := jsonutil.EncodeJSONAndCompress(table, nil)
	if err != nil {
		c.logger.Error("core: failed to encode and/or compress the mount table", "error", err)
		return err
	}

	// Create an entry
	entry := &Entry{
		Key:   coreMountConfigPath,
		Value: compressedBytes,
	}

	// Write to the physical backend
	if err := c.barrier.Put(entry); err != nil {
		c.logger.Error("core: failed to persist mount table", "error", err)
		return err
	}
	return nil
}
コード例 #2
0
ファイル: util.go プロジェクト: quixoten/vault
// GenerateForwardedRequest generates a new http.Request that contains the
// original requests's information in the new request's body.
func GenerateForwardedHTTPRequest(req *http.Request, addr string) (*http.Request, error) {
	fq, err := GenerateForwardedRequest(req)
	if err != nil {
		return nil, err
	}

	var newBody []byte
	switch os.Getenv("VAULT_MESSAGE_TYPE") {
	case "json":
		newBody, err = jsonutil.EncodeJSON(fq)
	case "json_compress":
		newBody, err = jsonutil.EncodeJSONAndCompress(fq, &compressutil.CompressionConfig{
			Type: compressutil.CompressionTypeLzw,
		})
	case "proto3":
		fallthrough
	default:
		newBody, err = proto.Marshal(fq)
	}
	if err != nil {
		return nil, err
	}

	ret, err := http.NewRequest("POST", addr, bytes.NewBuffer(newBody))
	if err != nil {
		return nil, err
	}

	return ret, nil
}
コード例 #3
0
ファイル: forwarded_request.go プロジェクト: mhurne/vault
// GenerateForwardedRequest generates a new http.Request that contains the
// original requests's information in the new request's body.
func GenerateForwardedRequest(req *http.Request, addr string) (*http.Request, error) {
	fq := ForwardedRequest{
		Method:          req.Method,
		URL:             req.URL,
		Header:          req.Header,
		Host:            req.Host,
		RemoteAddr:      req.RemoteAddr,
		ConnectionState: req.TLS,
	}

	buf := bytes.NewBuffer(nil)
	_, err := buf.ReadFrom(req.Body)
	if err != nil {
		return nil, err
	}
	fq.Body = buf.Bytes()

	newBody, err := jsonutil.EncodeJSONAndCompress(&fq, &compressutil.CompressionConfig{
		Type: compressutil.CompressionTypeLzw,
	})
	if err != nil {
		return nil, err
	}

	ret, err := http.NewRequest("POST", addr, bytes.NewBuffer(newBody))
	if err != nil {
		return nil, err
	}

	return ret, nil
}
コード例 #4
0
ファイル: mount.go プロジェクト: mhurne/vault
// persistMounts is used to persist the mount table after modification
func (c *Core) persistMounts(table *MountTable) error {
	if table.Type != mountTableType {
		c.logger.Printf(
			"[ERR] core: given table to persist has type %s but need type %s",
			table.Type,
			mountTableType)
		return fmt.Errorf("invalid table type given, not persisting")
	}

	for _, entry := range table.Entries {
		if entry.Table != table.Type {
			c.logger.Printf(
				"[ERR] core: entry in mount table with path %s has table value %s but is in table %s, refusing to persist",
				entry.Path,
				entry.Table,
				table.Type)
			return fmt.Errorf("invalid mount entry found, not persisting")
		}
	}

	// Encode the mount table into JSON and compress it (lzw).
	compressedBytes, err := jsonutil.EncodeJSONAndCompress(table, nil)
	if err != nil {
		c.logger.Printf("[ERR] core: failed to encode and/or compress the mount table: %v", err)
		return err
	}

	// Create an entry
	entry := &Entry{
		Key:   coreMountConfigPath,
		Value: compressedBytes,
	}

	// Write to the physical backend
	if err := c.barrier.Put(entry); err != nil {
		c.logger.Printf("[ERR] core: failed to persist mount table: %v", err)
		return err
	}
	return nil
}