// Fetch leaf cert(s) from Vault and apply them to RF targets.
Fetch leaf cert(s) from Vault and apply them to RF targets.
// Fetch leaf cert ( s ) from Vault and apply them to RF targets .
func doBMCSetCertsPost(w http.ResponseWriter, r *http.Request) {
	var jdata rfCertPost
	var retData rfCertPostRsp
	var certDomain string
	funcName := "doBMCSetCertsPost"
	err := getReqData(funcName, r, &jdata)
	if err != nil {
		emsg := fmt.Sprintf("ERROR: Problem getting request data: %v", err)
		sendErrorRsp(w, "Bad request data", emsg, r.URL.Path,
			http.StatusInternalServerError)
		return
	}
	td := makeTargData(jdata.Targets)
	_, tderr := hsmVerify(td, jdata.Force, false)
	if tderr != nil {
		emsg := fmt.Sprintf("ERROR: Problem verifying target/states: %v.", tderr)
		sendErrorRsp(w, "Indeterminate target/state", emsg, r.URL.Path,
			http.StatusInternalServerError)
		return
	}
	certDomain, derr := userDomainToCertDomain(jdata.CertDomain)
	if derr != nil {
		emsg := fmt.Sprintf("%v", err)
		sendErrorRsp(w, "Bad request data", emsg, r.URL.Path,
			http.StatusBadRequest)
		logger.Errorf("%s: %s", funcName, emsg)
		return
	}
	bads := make([]bool, len(jdata.Targets))
	certMap := make(map[string]*hms_certs.VaultCertData)
	domMap := make(map[string]*hms_certs.VaultCertData)
	var vcert hms_certs.VaultCertData
	var vcerr error
	for ix := 0; ix < len(jdata.Targets); ix++ {
		domID, err := hms_certs.CheckDomain([]string{jdata.Targets[ix]}, certDomain)
		if err != nil {
			crsp := certRsp{ID: jdata.Targets[ix],
				StatusCode: http.StatusBadRequest,
				StatusMsg: fmt.Sprintf("Cert target %s not found in domain %s",
					jdata.Targets[ix], jdata.CertDomain)}
			retData.Targets = append(retData.Targets, crsp)
			bads[ix] = true
			continue
		}
		dmap, ok := domMap[domID]
		if ok {
			logger.Tracef("%s: '%s' using already-fetched map for '%s'",
				funcName, jdata.Targets[ix], domID)
			certMap[jdata.Targets[ix]] = dmap
		} else {
			logger.Tracef("%s: '%s' fetching map for '%s'",
				funcName, jdata.Targets[ix], domID)
			vcert, vcerr = hms_certs.FetchCertData(domID, certDomain)
			if vcerr != nil {
				crsp := certRsp{ID: jdata.Targets[ix],
					StatusCode: http.StatusInternalServerError,
					StatusMsg: fmt.Sprintf("ERROR fetching cert for '%s', domain '%s': %v",
						jdata.Targets[ix], jdata.CertDomain, vcerr)}
				retData.Targets = append(retData.Targets, crsp)
				bads[ix] = true
				continue
			}
			domMap[domID] = &vcert
			certMap[jdata.Targets[ix]] = &vcert
		}
	}
	var tlist []string
	var sourceTL trsapi.HttpTask
	for ix := 0; ix < len(jdata.Targets); ix++ {
		if bads[ix] {
			continue
		}
		tlist = append(tlist, jdata.Targets[ix])
	}
	sourceTL.Timeout = time.Duration(appParams.HTTPTimeout) * time.Second
	sourceTL.Request, _ = http.NewRequest(http.MethodGet, "", nil)
	taskList := tloc.CreateTaskList(&sourceTL, len(tlist))
	populateTaskList(taskList, tlist, RFROOT_API, http.MethodGet, nil)
	certs := make([]bmcCertData, len(taskList))
	for ii := 0; ii < len(taskList); ii++ {
		if bads[ii] {
			taskList[ii].Ignore = true
			continue
		}
		targ := targFromTask(&taskList[ii])
		certs[ii].Cert = certMap[targ].Data.Certificate
		certs[ii].Key = certMap[targ].Data.PrivateKey
	}
	certErr := setCerts(taskList, certs, &retData)
	if certErr != nil {
		emsg := fmt.Sprintf("ERROR: Certificate set operation failed: %v",
			certErr)
		sendErrorRsp(w, "Certificate set operation error", emsg, r.URL.Path,
			http.StatusInternalServerError)
		return
	}
	ba, berr := json.Marshal(&retData)
	if berr != nil {
		emsg := fmt.Sprintf("ERROR: Problem marshaling return data.")
		sendErrorRsp(w, "JSON marshal error", emsg, r.URL.Path,
			http.StatusInternalServerError)
		return
	}
	w.Header().Set(CT_TYPE, CT_APPJSON)
	w.WriteHeader(http.StatusOK)
	w.Write(ba)
}
func doBMCSetCertsPost ( w http . ResponseWriter , r * http . Request ) { var jdata rfCertPost var retData rfCertPostRsp var certDomain string funcName := " " err := getReqData ( funcName , r , & jdata ) if err != nil { emsg := fmt . Sprintf ( " " , err ) sendErrorRsp ( w , " " , emsg , r . URL . Path , http . StatusInternalServerError ) return } td := makeTargData ( jdata . Targets ) _ , tderr := hsmVerify ( td , jdata . Force , false ) if tderr != nil { emsg := fmt . Sprintf ( " " , tderr ) sendErrorRsp ( w , " " , emsg , r . URL . Path , http . StatusInternalServerError ) return } certDomain , derr := userDomainToCertDomain ( jdata . CertDomain ) if derr != nil { emsg := fmt . Sprintf ( " " , err ) sendErrorRsp ( w , " " , emsg , r . URL . Path , http . StatusBadRequest ) logger . Errorf ( " " , funcName , emsg ) return } bads := make ( [ ] bool , len ( jdata . Targets ) ) certMap := make ( map [ string ] * hms_certs . VaultCertData ) domMap := make ( map [ string ] * hms_certs . VaultCertData ) var vcert hms_certs . VaultCertData var vcerr error for ix := 0 ; ix < len ( jdata . Targets ) ; ix ++ { domID , err := hms_certs . CheckDomain ( [ ] string { jdata . Targets [ ix ] } , certDomain ) if err != nil { crsp := certRsp { ID : jdata . Targets [ ix ] , StatusCode : http . StatusBadRequest , StatusMsg : fmt . Sprintf ( " " , jdata . Targets [ ix ] , jdata . CertDomain ) } retData . Targets = append ( retData . Targets , crsp ) bads [ ix ] = true continue } dmap , ok := domMap [ domID ] if ok { logger . Tracef ( " " , funcName , jdata . Targets [ ix ] , domID ) certMap [ jdata . Targets [ ix ] ] = dmap } else { logger . Tracef ( " " , funcName , jdata . Targets [ ix ] , domID ) vcert , vcerr = hms_certs . FetchCertData ( domID , certDomain ) if vcerr != nil { crsp := certRsp { ID : jdata . Targets [ ix ] , StatusCode : http . StatusInternalServerError , StatusMsg : fmt . Sprintf ( " " , jdata . Targets [ ix ] , jdata . CertDomain , vcerr ) } retData . Targets = append ( retData . Targets , crsp ) bads [ ix ] = true continue } domMap [ domID ] = & vcert certMap [ jdata . Targets [ ix ] ] = & vcert } } var tlist [ ] string var sourceTL trsapi . HttpTask for ix := 0 ; ix < len ( jdata . Targets ) ; ix ++ { if bads [ ix ] { continue } tlist = append ( tlist , jdata . Targets [ ix ] ) } sourceTL . Timeout = time . Duration ( appParams . HTTPTimeout ) * time . Second sourceTL . Request , _ = http . NewRequest ( http . MethodGet , " " , nil ) taskList := tloc . CreateTaskList ( & sourceTL , len ( tlist ) ) populateTaskList ( taskList , tlist , RFROOT_API , http . MethodGet , nil ) certs := make ( [ ] bmcCertData , len ( taskList ) ) for ii := 0 ; ii < len ( taskList ) ; ii ++ { if bads [ ii ] { taskList [ ii ] . Ignore = true continue } targ := targFromTask ( & taskList [ ii ] ) certs [ ii ] . Cert = certMap [ targ ] . Data . Certificate certs [ ii ] . Key = certMap [ targ ] . Data . PrivateKey } certErr := setCerts ( taskList , certs , & retData ) if certErr != nil { emsg := fmt . Sprintf ( " " , certErr ) sendErrorRsp ( w , " " , emsg , r . URL . Path , http . StatusInternalServerError ) return } ba , berr := json . Marshal ( & retData ) if berr != nil { emsg := fmt . Sprintf ( " " ) sendErrorRsp ( w , " " , emsg , r . URL . Path , http . StatusInternalServerError ) return } w . Header ( ) . Set ( CT_TYPE , CT_APPJSON ) w . WriteHeader ( http . StatusOK ) w . Write ( ba ) }
Fetch leaf cert(s) from Vault and apply them to RF targets.
Fetch leaf cert ( s ) from Vault and apply them to RF targets .
//Verify targets //Verify the cert domain //Resolve all targets to domainIDs; make a map 'twixt targets and //domainIDs; fetch all relevant domain certs and place into the map. //Grab the cert from Vault //Call setCerts() to do the dirty work using the map to get the right cert //data. //Make cert/key list