As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Commit 1fa30cb0 authored by James Sandlin's avatar James Sandlin
Browse files

waitgroup implemented & otp testing works

parent c93f5456
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -148,87 +148,132 @@ func (c *Command) Execute(ctx context.Context) error {
return err
}
// Create timeout context
myctx, mycancel := context.WithCancel(ctx)
defer mycancel()
waitGroup := sync.WaitGroup{}
myctx, cancelCtx := context.WithTimeout(ctx, ctxMaxTime)
defer cancelCtx()
//myctx, mycancel := context.WithCancel(timeoutCtx)
// Also allow manual OTP entry while waiting for push, with same timeout as push
otpChannel := make(chan Result)
waitGroup.Add(1)
//defer close(otpChannel)
otpAuth := make(chan Result)
go func() {
defer close(otpAuth)
defer waitGroup.Done()
ctxlog.Info("twofactorverify: execute: waiting for user input")
otpAnswer := c.getOTP(myctx)
select {
case <-ctx.Done(): // manual OTP cancelled by push
otpAuth <- Result{Error: nil, Status: "cancelled", Success: false}
otpChannel <- Result{Error: nil, Status: "cancelled", Success: false}
default:
status, success, err := c.verifyOTP(myctx, otpAnswer)
otpAuth <- Result{Error: err, Status: status, Success: success}
otpChannel <- Result{Error: err, Status: status, Success: success}
}
//cancelCtx()
}()
for {
select {
case res := <- otpAuth:
if len(res.Status) > 0 && res.Status != "cancelled"{
fmt.Fprint(c.ReadWriter.Out, res.Status)
return nil
}
}
}
return nil
}
func (c Command) processCmd(ctx context.Context, cancelTimeout context.CancelFunc) (result Result) {
ctxlog := log.ContextLogger(ctx)
otpAuth := make(chan Result)
//// Background push notification with timeout
pushChannel := make(chan Result)
waitGroup.Add(1)
go func() {
defer close(otpAuth)
ctxlog.Info("twofactorverify: execute: waiting for user input")
otpAnswer := c.getOTP(ctx)
defer waitGroup.Done()
//defer close(pushChannel)
ctxlog.Info("twofactorverify: execute: waiting for push auth")
//status, success, err := c.pushAuth(myctx)
//ctxlog.WithError(err).Info("twofactorverify: execute: push auth verified")
select {
case <-ctx.Done(): // manual OTP cancelled by push
fmt.Println("otpAuth.ctx.Done()")
otpAuth <- Result{Error: nil, Status: "cancelled", Success: false}
fmt.Println("----------------------------------------------------")
fmt.Println("otpAuth = ", otpAuth)
fmt.Println("----------------------------------------------------")
default:
fmt.Println("otpAuth.default")
cancelTimeout()
fmt.Println("Call c.verifyOTP(", ctx, ", ", otpAnswer, ")")
status, success, err := c.verifyOTP(ctx, otpAnswer)
fmt.Println("otpAnswer.status = ", status)
fmt.Println("otpAnswer.success = ", success)
fmt.Println("otpAnswer.err = ", err)
otpAuth <- Result{Error: err, Status: status, Success: success}
fmt.Println("----------------------------------------------------")
fmt.Println("otpAuth = ", otpAuth)
fmt.Println("----------------------------------------------------")
case <-myctx.Done(): // push cancelled by manual OTP
// skip writing to channel
pushChannel <- Result{Error: nil, Status: "cancelled", Success: false}
ctxlog.Info("twofactorverify: execute: push auth cancelled")
//default:
// pushChannel <- Result{Error: err, Status: status, Success: success}
}
}()
for {
//fmt.Println("for loop")
//for {
select {
case res := <- otpAuth:
fmt.Println(res)
//fmt.Println("-------------")
//fmt.Println("otpAuth = ", ores)
//fmt.Println("-------------")
if len(res.Status) > 0 && res.Status != "cancelled"{
//fmt.Println("-------------")
//fmt.Println("otpAuth = ", res.Status)
//fmt.Println("-------------")
return res
case res := <-otpChannel:
//fmt.Println("Received from otpChannel => ", res)
if len(res.Status) > 0 && res.Status != "cancelled" {
fmt.Fprint(c.ReadWriter.Out, res.Status)
return nil
}
case res := <-pushChannel:
if len(res.Status) > 0 && res.Status != "cancelled" {
//fmt.Println("Received from pushChannel => ", res)
fmt.Println("res.Status == ", res.Status, " -> ", len(res.Status))
// //fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
// //fmt.Println(res)
// //fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
fmt.Fprint(c.ReadWriter.Out, res.Status)
return nil
}
//case <- myctx.Done():
// fmt.Fprint(c.ReadWriter.Out, "\nOTP verification timed out\n")
// return nil
}
}
return
}
waitGroup.Wait()
//}
return nil
}
//
//func (c Command) processCmd(ctx context.Context, cancelTimeout context.CancelFunc) (result Result) {
// ctxlog := log.ContextLogger(ctx)
//
// otpAuth := make(chan Result)
// go func() {
// defer close(otpAuth)
// ctxlog.Info("twofactorverify: execute: waiting for user input")
// otpAnswer := c.getOTP(ctx)
//
// select {
// case <-ctx.Done(): // manual OTP cancelled by push
// fmt.Println("otpAuth.ctx.Done()")
// otpAuth <- Result{Error: nil, Status: "cancelled", Success: false}
// fmt.Println("----------------------------------------------------")
// fmt.Println("otpAuth = ", otpAuth)
// fmt.Println("----------------------------------------------------")
// default:
// fmt.Println("otpAuth.default")
// cancelTimeout()
// fmt.Println("Call c.verifyOTP(", ctx, ", ", otpAnswer, ")")
// status, success, err := c.verifyOTP(ctx, otpAnswer)
// fmt.Println("otpAnswer.status = ", status)
// fmt.Println("otpAnswer.success = ", success)
// fmt.Println("otpAnswer.err = ", err)
// otpAuth <- Result{Error: err, Status: status, Success: success}
// fmt.Println("----------------------------------------------------")
// fmt.Println("otpAuth = ", otpAuth)
// fmt.Println("----------------------------------------------------")
// }
// }()
// for {
// //fmt.Println("for loop")
// select {
// case res := <- otpAuth:
// fmt.Println(res)
// //fmt.Println("-------------")
// //fmt.Println("otpAuth = ", ores)
// //fmt.Println("-------------")
// if len(res.Status) > 0 && res.Status != "cancelled"{
// //fmt.Println("-------------")
// //fmt.Println("otpAuth = ", res.Status)
// //fmt.Println("-------------")
// return res
// }
// }
// }
// return
//}
//
//
func (c *Command) getOTP(ctx context.Context) string {
Loading
Loading
@@ -248,13 +293,13 @@ func (c *Command) getOTP(ctx context.Context) string {
func (c *Command) verifyOTP(ctx context.Context, otp string) (status string, success bool, err error) {
reason := ""
fmt.Println("verifyOTP(", ctx, ", ", c.Args, ", ",otp,")")
//fmt.Println("verifyOTP(", ctx, ", ", c.Args, ", ",otp,")")
success, reason, err = c.Client.VerifyOTP(ctx, c.Args, otp)
fmt.Println("----------------------------------------------------")
fmt.Println("verifyOTP.status = ", status)
fmt.Println("verifyOTP.success = ", success)
fmt.Println("verifyOTP.err = ", err)
fmt.Println("----------------------------------------------------")
//fmt.Println("----------------------------------------------------")
//fmt.Println("verifyOTP.status = ", status)
//fmt.Println("verifyOTP.success = ", success)
//fmt.Println("verifyOTP.err = ", err)
//fmt.Println("----------------------------------------------------")
if success {
status = fmt.Sprintf("\nOTP validation successful. Git operations are now allowed.\n")
} else {
Loading
Loading
Loading
Loading
@@ -44,11 +44,11 @@ func (c *Client) VerifyOTP(ctx context.Context, args *commandargs.Shell, otp str
}
response, err := c.client.Post(ctx, "/two_factor_manual_otp_check", requestBody)
fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
fmt.Println("c.client = ", c.client)
fmt.Println("client.VerifyOTP.response = ", response)
fmt.Println("client.VerifyOTP.err = ", err)
fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
//fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
//fmt.Println("c.client = ", c.client)
//fmt.Println("client.VerifyOTP.response = ", response)
//fmt.Println("client.VerifyOTP.err = ", err)
//fmt.Println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
if err != nil {
return false, "", err
}
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment