github.com/grailbio/base@v0.0.11/cmd/grail-access/remote/receive.go (about)

     1  // Copyright 2022 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package remote
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  
    11  	v23 "v.io/v23"
    12  	"v.io/v23/context"
    13  	"v.io/v23/security"
    14  )
    15  
    16  // ReceiveBlessings reads encoded blessings from r and sets them as the default
    17  // blessings and as blessings for all principal peers.
    18  func ReceiveBlessings(ctx *context.T, r io.Reader) error {
    19  	p := v23.GetPrincipal(ctx)
    20  	if p == nil {
    21  		// We rely on the caller to set up the principal before making this
    22  		// call.
    23  		return fmt.Errorf("no local principal to bless")
    24  	}
    25  	// Read a single-line encoding of the received blessing, and set them as
    26  	// both the default and for all peer principals.
    27  	input, err := io.ReadAll(r)
    28  	if err != nil {
    29  		return fmt.Errorf("reading input: %v", err)
    30  	}
    31  	b, err := decodeBlessings(string(input))
    32  	if err != nil {
    33  		return fmt.Errorf("decoding blessings string: %v", err)
    34  	}
    35  	store := p.BlessingStore()
    36  	if err := store.SetDefault(b); err != nil {
    37  		return fmt.Errorf("setting blessings %v as default: %v", b, err)
    38  	}
    39  	if _, err := store.Set(b, security.AllPrincipals); err != nil {
    40  		return fmt.Errorf("setting blessings %v for peers %v: %v", b, security.AllPrincipals, err)
    41  	}
    42  	if err := security.AddToRoots(p, b); err != nil {
    43  		return fmt.Errorf("adding blessings to recognized roots: %v", err)
    44  	}
    45  	return nil
    46  }