|
|
|
|
@ -7,6 +7,7 @@ import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
tfe "github.com/hashicorp/go-tfe"
|
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
|
@ -146,21 +147,30 @@ func (b *Remote) opApply(stopCtx, cancelCtx context.Context, op *backend.Operati
|
|
|
|
|
return r, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Operation, r *tfe.Run) error {
|
|
|
|
|
func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Operation, r *tfe.Run) (err error) {
|
|
|
|
|
if b.CLI != nil {
|
|
|
|
|
b.CLI.Output("\n------------------------------------------------------------------------\n")
|
|
|
|
|
}
|
|
|
|
|
for _, pc := range r.PolicyChecks {
|
|
|
|
|
logs, err := b.client.PolicyChecks.Logs(stopCtx, pc.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return generalError("error retrieving policy check logs", err)
|
|
|
|
|
}
|
|
|
|
|
scanner := bufio.NewScanner(logs)
|
|
|
|
|
// Loop until the context is canceled or the policy check is finished.
|
|
|
|
|
for {
|
|
|
|
|
pc, err = b.client.PolicyChecks.Read(stopCtx, pc.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return generalError("error retrieving policy check", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Retrieve the policy check to get its current status.
|
|
|
|
|
pc, err := b.client.PolicyChecks.Read(stopCtx, pc.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return generalError("error retrieving policy check", err)
|
|
|
|
|
switch pc.Status {
|
|
|
|
|
case tfe.PolicyPending, tfe.PolicyQueued:
|
|
|
|
|
select {
|
|
|
|
|
case <-stopCtx.Done():
|
|
|
|
|
return generalError("error retrieving policy check", stopCtx.Err())
|
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Break if the policy check is finished.
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var msgPrefix string
|
|
|
|
|
@ -173,10 +183,25 @@ func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Ope
|
|
|
|
|
msgPrefix = fmt.Sprintf("Unknown policy check (%s)", pc.Scope)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Don't show the full policy output if the policy passed.
|
|
|
|
|
if pc.Status == tfe.PolicyPasses {
|
|
|
|
|
if b.CLI != nil {
|
|
|
|
|
b.CLI.Output(b.Colorize().Color(msgPrefix + ": passed\n"))
|
|
|
|
|
b.CLI.Output("------------------------------------------------------------------------")
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if b.CLI != nil {
|
|
|
|
|
b.CLI.Output(b.Colorize().Color(msgPrefix + ":\n"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logs, err := b.client.PolicyChecks.Logs(stopCtx, pc.ID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return generalError("error retrieving policy check logs", err)
|
|
|
|
|
}
|
|
|
|
|
scanner := bufio.NewScanner(logs)
|
|
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
if b.CLI != nil {
|
|
|
|
|
b.CLI.Output(b.Colorize().Color(scanner.Text()))
|
|
|
|
|
@ -187,11 +212,6 @@ func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Ope
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch pc.Status {
|
|
|
|
|
case tfe.PolicyPasses:
|
|
|
|
|
if b.CLI != nil {
|
|
|
|
|
b.CLI.Output("\n------------------------------------------------------------------------")
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
case tfe.PolicyErrored:
|
|
|
|
|
return fmt.Errorf(msgPrefix + " errored.")
|
|
|
|
|
case tfe.PolicyHardFailed:
|
|
|
|
|
@ -215,13 +235,13 @@ func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Ope
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if b.CLI != nil {
|
|
|
|
|
b.CLI.Output("------------------------------------------------------------------------")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err = b.client.PolicyChecks.Override(stopCtx, pc.ID); err != nil {
|
|
|
|
|
return generalError("error overriding policy check", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if b.CLI != nil {
|
|
|
|
|
b.CLI.Output("------------------------------------------------------------------------")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|