// ensure keys are created using a control socket
484639
func (k *Keyfile) ensure(nc net.Conn) (err error) {
	if len(k.fname) > 0 {
		_, err = os.Stat(k.fname)
	}
	if os.IsNotExist(err) || len(k.fname) == 0 {
		// no keyfile
		_, err = fmt.Fprintf(nc, "DEST GENERATE\n")
		r := bufio.NewReader(nc)
		var line string
		line, err = r.ReadString(10)
		if err == nil {
			sc := bufio.NewScanner(strings.NewReader(line))
			sc.Split(bufio.ScanWords)
			for sc.Scan() {
				txt := sc.Text()
				upper := strings.ToUpper(txt)
				if upper == "DEST" {
					continue
				}
				if upper == "REPLY" {
					continue
				}
				if strings.HasPrefix(upper, "PUB=") {
					k.pubkey = txt[4:]
					continue
				}
				if strings.HasPrefix(upper, "PRIV=") {
					k.privkey = txt[5:]
					continue
				}
			}
			// store new keys
			err = k.Store()
			return
		}
	}
	// load keys
	err = k.Load()
	return
}
1028353
func ensureValidToken(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
	md, ok := metadata.FromIncomingContext(ctx)
	if !ok {
		return nil, errMissingMetadata
	}
	// The keys within metadata.MD are normalized to lowercase.
	// See: https://godoc.org/google.golang.org/grpc/metadata#New
	if !valid(md["authorization"]) {
		return nil, errInvalidToken
	}
	// Continue execution of handler after ensuring a valid token.
	return handler(ctx, req)
}
1354063
func EnsureKVMs(c context.Context, cfgs []*config.Datacenter, platformIds, rackIds map[string]int64) (map[string]int64, error) {
	t := &KVMsTable{}
	t.platforms = platformIds
	t.racks = rackIds
	if err := t.fetch(c); err != nil {
		return nil, errors.Annotate(err, "failed to fetch KVMs").Err()
	}
	if err := t.computeChanges(c, cfgs); err != nil {
		return nil, errors.Annotate(err, "failed to compute changes").Err()
	}
	if err := t.add(c); err != nil {
		return nil, errors.Annotate(err, "failed to add KVMs").Err()
	}
	if err := t.remove(c); err != nil {
		return nil, errors.Annotate(err, "failed to remove KVMs").Err()
	}
	if err := t.update(c); err != nil {
		return nil, errors.Annotate(err, "failed to update KVMs").Err()
	}
	return t.ids(c), nil
}
1294177
func (s *Server) EnsureIPv4(netName string) (bool, error) {
	var modified bool

	net, eTag, err := s.GetNetwork(netName)
	if err != nil {
		return false, errors.Trace(err)
	}

	cfg, ok := net.Config["ipv4.address"]
	if !ok || cfg == "none" {
		if net.Config == nil {
			net.Config = make(device, 2)
		}
		net.Config["ipv4.address"] = "auto"
		net.Config["ipv4.nat"] = "true"

		if err := s.UpdateNetwork(netName, net.Writable(), eTag); err != nil {
			return false, errors.Trace(err)
		}
		modified = true
	}

	return modified, nil
}
1034664
func (e *Kex2Provisionee) saveKeys(m libkb.MetaContext) error {
	_, err := libkb.WriteLksSKBToKeyring(m, e.eddsa, e.lks)
	if err != nil {
		return err
	}
	_, err = libkb.WriteLksSKBToKeyring(m, e.dh, e.lks)
	if err != nil {
		return err
	}
	return nil
}
1340024
func (c *Config) KeyPair() (*tls.Certificate, error) {
	return loadKeyPair(c.CertFile, c.KeyFile)
}
857668
func (client *Client) ensureProperties() error {
	var msg string

	if client.endpoint == "" {
		msg = fmt.Sprintf("endpoint cannot be empty!")
	} else if client.version == "" {
		msg = fmt.Sprintf("version cannot be empty!")
	} else if client.AccessKeyId == "" {
		msg = fmt.Sprintf("AccessKeyId cannot be empty!")
	} else if client.AccessKeySecret == "" {
		msg = fmt.Sprintf("AccessKeySecret cannot be empty!")
	}

	if msg != "" {
		return errors.New(msg)
	}

	return nil
}
392913
func Ensure(fname string) (err error) {
  if ! checkfile(fname) {
    // does not exist generate file now
    err = Save(fname)
  }
  if err == nil {
    err = Load(fname)
  }
  return
}
89840
func (k *Kubernetes) ensurePKIRolesEtcd(p *PKIVaultBackend) error {
	var result *multierror.Error

	if err := p.WriteRole(k.etcdClientRole()); err != nil {
		result = multierror.Append(result, err)
	}

	if err := p.WriteRole(k.etcdServerRole()); err != nil {
		result = multierror.Append(result, err)
	}

	return result.ErrorOrNil()
}
906387
func keyPrefix(key []byte) (pre prefix) {
	if len(key) >= prefixSize {
		return *(*prefix)(unsafe.Pointer(&key[0]))
	}
	copy(pre[:], key)
	return pre
}
1228989
func (e *loginProvision) ensureLKSec(m libkb.MetaContext) error {
	if e.lks != nil {
		return nil
	}

	pps, err := e.ppStream(m)
	if err != nil {
		return err
	}

	e.lks = libkb.NewLKSec(pps, e.arg.User.GetUID())
	return nil
}
292663
func (n *Network) EnsureItExists(ctx context.Context) error {
	networkResource, err := n.Inspect(ctx)
	if n.external {
		if client.IsErrNotFound(err) {
			// FIXME(vdemeester) introduce some libcompose error type
			return fmt.Errorf("Network %s declared as external, but could not be found. Please create the network manually using docker network create %s and try again", n.fullName(), n.fullName())
		}
		return err
	}
	if err != nil && client.IsErrNotFound(err) {
		return n.create(ctx)
	}
	if n.driver != "" && networkResource.Driver != n.driver {
		return fmt.Errorf("Network %q needs to be recreated - driver has changed", n.fullName())
	}
	if len(n.driverOptions) != 0 && !reflect.DeepEqual(networkResource.Options, n.driverOptions) {
		return fmt.Errorf("Network %q needs to be recreated - options have changed", n.fullName())
	}
	return err
}
890461
func (instance *HmacKeyBackend) ProvideKey(token *jwt.Token) (interface{}, error) {
	if err := AssertHmacToken(token); err != nil {
		return nil, err
	}
	return instance.secret, nil
}
1006289
func (s *SSHService) CreateSSHKeyPair(p *CreateSSHKeyPairParams) (*CreateSSHKeyPairResponse, error) {
	resp, err := s.cs.newRequest("createSSHKeyPair", p.toURLValues())
	if err != nil {
		return nil, err
	}

	if resp, err = getRawValue(resp); err != nil {
		return nil, err
	}

	var r CreateSSHKeyPairResponse
	if err := json.Unmarshal(resp, &r); err != nil {
		return nil, err
	}

	return &r, nil
}
293675
func (c *Client) GenerateKeyPair(pass string) ([]byte, []byte, error) {
	out, err := c.PostJSON(c.Endpoint("keypair"), generateKeyPairReq{Password: pass})
	if err != nil {
		return nil, nil, trace.Wrap(err)
	}
	var kp *generateKeyPairResponse
	if err := json.Unmarshal(out.Bytes(), &kp); err != nil {
		return nil, nil, err
	}
	return kp.PrivKey, []byte(kp.PubKey), err
}
681317
func checkKeyValid(accessKey string) (auth.Credentials, bool, APIErrorCode) {
	var owner = true
	var cred = globalServerConfig.GetCredential()
	if cred.AccessKey != accessKey {
		if globalIAMSys == nil {
			return cred, false, ErrInvalidAccessKeyID
		}
		// Check if the access key is part of users credentials.
		var ok bool
		if cred, ok = globalIAMSys.GetUser(accessKey); !ok {
			return cred, false, ErrInvalidAccessKeyID
		}
		owner = false
	}
	return cred, owner, ErrNone
}
657450
func EnsureRackKVMs(c context.Context, cfgs []*config.Datacenter, kvmIds map[string]int64) error {
	t := &RackKVMsTable{}
	t.kvms = kvmIds
	if err := t.fetch(c); err != nil {
		return errors.Annotate(err, "failed to fetch racks").Err()
	}
	if err := t.computeChanges(c, cfgs); err != nil {
		return errors.Annotate(err, "failed to compute changes").Err()
	}
	if err := t.update(c); err != nil {
		return errors.Annotate(err, "failed to update racks").Err()
	}
	return nil
}