Skip to content

Commit

Permalink
Oauth quickfix (#1437)
Browse files Browse the repository at this point in the history
* quick fix for oauth

* fix log
  • Loading branch information
jlmorris3827 authored Apr 15, 2021
1 parent 68f071c commit 8eb6021
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
6 changes: 3 additions & 3 deletions crm-platforms/vcd/vcd-agw-sim/vcd-agw-sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ func doApi(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
stoken := strings.Split(token, "Bearer")
if len(stoken) != 2 {
log.Printf("Bad access token: %s", token)
log.Printf("Bad access token, no bearer: %s", token)
w.WriteHeader(http.StatusBadRequest)
return
}
tokval := strings.TrimSpace(stoken[1])
if tokval != "simulatoraccesstoken" {
log.Printf("Bad access token: %s", tokval)
if !strings.HasPrefix(tokval, "simulatoraccesstoken") {
log.Printf("Bad access token, wrong value: %s", tokval)
w.WriteHeader(http.StatusUnauthorized)
return
}
Expand Down
24 changes: 21 additions & 3 deletions crm-platforms/vcd/vcd-security.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type vcdClientInfo struct {
var cloudletClients map[edgeproto.CloudletKey]*vcdClientInfo
var cloudletClientLock sync.Mutex

var maxOauthTokenReadyTime = time.Second * 60

// vcd security related operations

func init() {
Expand Down Expand Up @@ -210,6 +212,7 @@ func (v *VcdPlatform) GetClient(ctx context.Context, creds *VcdConfigParams) (cl
if creds.OauthAgwUrl != "" {
apiUrl = creds.OauthAgwUrl
}
newOauthToken := false
u, err := url.ParseRequestURI(apiUrl)
if err != nil {
return nil, fmt.Errorf("Unable to parse request to org %s at %s err: %s", creds.Org, creds.VcdApiUrl, err)
Expand Down Expand Up @@ -277,6 +280,7 @@ func (v *VcdPlatform) GetClient(ctx context.Context, creds *VcdConfigParams) (cl
delete(cloudletClients, *v.vmProperties.CommonPf.PlatformConfig.CloudletKey)
return nil, fmt.Errorf("failed oauth response %s at %s err: %s", creds.Org, creds.OauthSgwUrl, err)
}
newOauthToken = true
}
clientInfo.lastUpdateTime = time.Now()
}
Expand All @@ -285,9 +289,23 @@ func (v *VcdPlatform) GetClient(ctx context.Context, creds *VcdConfigParams) (cl
return nil, fmt.Errorf("CopyClient failed - %v", err)
}
// always refresh the vcd session token
_, err = clientCopy.GetAuthResponse(creds.User, creds.Password, creds.Org)
if err != nil {
log.SpanLog(ctx, log.DebugLevelInfra, "Unable to login to org", "org", creds.Org, "err", err)
start := time.Now()
for {
_, err = clientCopy.GetAuthResponse(creds.User, creds.Password, creds.Org)
if err == nil {
break
}
log.SpanLog(ctx, log.DebugLevelInfra, "Error logging into org", "org", creds.Org, "err", err)
if newOauthToken {
// if we just got a new oauth token, it may not be ready for us to use. Give
// it a little time. This is a workaround pending a more complete fix.
elapsed := time.Since(start)
if elapsed < maxOauthTokenReadyTime {
log.SpanLog(ctx, log.DebugLevelInfra, "sleeping 3 seconds to retry oauth token", "org", creds.Org, "err", err)
time.Sleep(3 * time.Second)
continue
}
}
delete(cloudletClients, *v.vmProperties.CommonPf.PlatformConfig.CloudletKey)
return nil, fmt.Errorf("failed oauth response %s at %s err: %s", creds.Org, creds.OauthSgwUrl, err)
}
Expand Down
7 changes: 6 additions & 1 deletion crm-platforms/vcd/vcd-sgw-sim/vcd-sgw-sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/mobiledgex/edge-cloud-infra/crm-platforms/vcd"
)

var currTokenNum uint32 = 1

var (
port = flag.Int("port", 8443, "listen port")
expiresin = flag.Int("expiresin", 28800, "expires in seconds")
Expand Down Expand Up @@ -90,16 +92,19 @@ func getToken(w http.ResponseWriter, r *http.Request) {
log.Println("doing getToken")
code := validateRequest(r)
if code != http.StatusOK {
log.Printf("request validation failed - code: %d", code)

w.WriteHeader(code)
return
}
tokenResponse := vcd.TokenResponse{
AccessToken: "simulatoraccesstoken",
AccessToken: fmt.Sprintf("simulatoraccesstoken-%d", currTokenNum),
TokenType: "Bearer",
ExpiresIn: *expiresin,
Scope: "openid account.read customer.read customer.accounts.read",
IdToken: "aaaaaaaa.bbbbbbbb.cccccccc",
}
currTokenNum++
byt, _ := json.Marshal(tokenResponse)
log.Printf("<===== Sent response: %v\n", tokenResponse)
w.Write(byt)
Expand Down

0 comments on commit 8eb6021

Please sign in to comment.