Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TT-11960]fix panic in Oas when using mode public #6241

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 18 additions & 29 deletions gateway/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@
oAuthClientTokensKeyPattern = "oauth-data.*oauth-client-tokens.*"
)

var (
ErrRequestMalformed = errors.New("request malformed")
)
var ErrRequestMalformed = errors.New("request malformed")

// apiModifyKeySuccess represents when a Key modification was successful
//
Expand Down Expand Up @@ -143,14 +141,12 @@
}

func doJSONExport(w http.ResponseWriter, code int, obj interface{}, fileName string) {

if code != http.StatusOK {
doJSONWrite(w, code, obj)
return
}

stream, err := json.MarshalIndent(obj, "", " ")

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand All @@ -165,7 +161,6 @@
job := instrument.NewJob("SystemAPIError")
job.Event(err.Error())
}

}

type MethodNotAllowedHandler struct{}
Expand Down Expand Up @@ -508,7 +503,7 @@
keyName = gw.generateToken(newSession.OrgID, keyName)
}

//set the original expiry if the content in payload is a past time
// set the original expiry if the content in payload is a past time
if time.Now().After(time.Unix(newSession.Expires, 0)) && newSession.Expires > 1 {
newSession.Expires = originalKey.Expires
}
Expand Down Expand Up @@ -839,12 +834,10 @@
}

func (gw *Gateway) handleDeleteHashedKey(keyName, orgID, apiID string, resetQuota bool) (interface{}, int) {

session, ok := gw.GlobalSessionManager.SessionDetail(orgID, keyName, true)
keyName = session.KeyID
if !ok {
return apiError("There is no such key found"), http.StatusNotFound

}

if apiID == "-1" {
Expand Down Expand Up @@ -939,7 +932,7 @@
return apiError("Marshalling failed"), http.StatusInternalServerError
}

if err := ioutil.WriteFile(polFilePath, asByte, 0644); err != nil {
if err := ioutil.WriteFile(polFilePath, asByte, 0o644); err != nil {
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
log.Error("Failed to create file! - ", err)
return apiError("Failed to create file!"), http.StatusInternalServerError
}
Expand Down Expand Up @@ -1039,10 +1032,11 @@

obj, code := gw.handleGetAPI(apiID, true)
if apiOAS, ok := obj.(*oas.OAS); ok && modePublic {
apiOAS.RemoveTykExtension()
oasCopy := *apiOAS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use OAS.Clone for a deep copy.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use OAS.Clone for a deep copy.

@titpetric I have tried to use OAS.Clone but when I use OAS.Clone it still panics. I am not sure why

oasCopy.RemoveTykExtension()
return oasCopy, code
}
return obj, code

}

func (gw *Gateway) handleAddApi(r *http.Request, fs afero.Fs, oasEndpoint bool) (interface{}, int) {
Expand Down Expand Up @@ -1266,7 +1260,7 @@
return errors.New("marshalling failed"), http.StatusInternalServerError
}

if err := ioutil.WriteFile(defFilePath, asByte, 0644); err != nil {
if err := ioutil.WriteFile(defFilePath, asByte, 0o644); err != nil {
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
log.Infof("EL file path: %v", defFilePath)
log.Error("Failed to create file! - ", err)
return errors.New("file object creation failed, write error"), http.StatusInternalServerError
Expand Down Expand Up @@ -1503,7 +1497,6 @@
}

reqBodyInBytes, oasObj, err := extractOASObjFromReq(r.Body)

if err != nil {
doJSONWrite(w, http.StatusBadRequest, apiError(err.Error()))
return
Expand Down Expand Up @@ -2049,7 +2042,6 @@
doJSONWrite(w, http.StatusBadRequest, apiError("Failed to create key, keys must have at least one Access Rights record set."))
return
}

}

obj := apiModifyKeySuccess{
Expand Down Expand Up @@ -2484,7 +2476,6 @@
}

func (gw *Gateway) rotateOauthClientHandler(w http.ResponseWriter, r *http.Request) {

apiID := mux.Vars(r)["apiID"]
keyName := mux.Vars(r)["keyName"]

Expand All @@ -2498,7 +2489,7 @@
appID := mux.Vars(r)["appID"]
orgID := r.FormValue("orgID")

//get all organization apis
// get all organization apis
apisIds := gw.getApisIdsForOrg(orgID)

for index := range apisIds {
Expand Down Expand Up @@ -2717,12 +2708,14 @@
return apiError("OAuth Client ID not found"), http.StatusNotFound
}

const oAuthNotPropagatedErr = "OAuth client list isn't available or hasn't been propagated yet."
const oAuthClientNotFound = "OAuth client not found"
const oauthClientIdEmpty = "client_id is required"
const oauthClientSecretEmpty = "client_secret is required"
const oauthClientSecretWrong = "client secret is wrong"
const oauthTokenEmpty = "token is required"
const (
oAuthNotPropagatedErr = "OAuth client list isn't available or hasn't been propagated yet."
oAuthClientNotFound = "OAuth client not found"
oauthClientIdEmpty = "client_id is required"
oauthClientSecretEmpty = "client_secret is required"
oauthClientSecretWrong = "client secret is wrong"
oauthTokenEmpty = "token is required"
)

func (gw *Gateway) getApiClients(apiID string) ([]ExtendedOsinClientInterface, apiStatusMessage, int) {
var err error
Expand Down Expand Up @@ -2759,7 +2752,6 @@

// List Clients
func (gw *Gateway) getOauthClients(apiID string) (interface{}, int) {

clientData, _, apiStatusCode := gw.getApiClients(apiID)

if apiStatusCode != 200 {
Expand Down Expand Up @@ -2872,7 +2864,6 @@

func (gw *Gateway) RevokeTokenHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()

if err != nil {
doJSONWrite(w, http.StatusBadRequest, apiError("cannot parse form. Form malformed"))
return
Expand Down Expand Up @@ -2937,7 +2928,6 @@

func (gw *Gateway) RevokeAllTokensHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()

if err != nil {
doJSONWrite(w, http.StatusBadRequest, apiError("cannot parse form. Form malformed"))
return
Expand All @@ -2959,7 +2949,7 @@

apis := gw.getApisForOauthClientId(clientId, orgId)
if len(apis) == 0 {
//if api is 0 is because the client wasn't found
// if api is 0 is because the client wasn't found
doJSONWrite(w, http.StatusNotFound, apiError("oauth client doesn't exist"))
return
}
Expand All @@ -2986,7 +2976,6 @@
func (gw *Gateway) validateOAS(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
reqBodyInBytes, oasObj, err := extractOASObjFromReq(r.Body)

if err != nil {
doJSONWrite(w, http.StatusBadRequest, apiError(err.Error()))
return
Expand Down Expand Up @@ -3070,6 +3059,7 @@
r2 := r.WithContext(ctx)
*r = *r2
}

func setCtxValue(r *http.Request, key, val interface{}) {
setContext(r, context.WithValue(r.Context(), key, val))
}
Expand Down Expand Up @@ -3420,7 +3410,6 @@

// invalidate tokens if we had a new policy
func invalidateTokens(prevClient ExtendedOsinClientInterface, updatedClient OAuthClient, oauthManager *OAuthManager) {

if prevPolicy := prevClient.GetPolicyID(); prevPolicy != "" && prevPolicy != updatedClient.PolicyID {
tokenList, err := oauthManager.OsinServer.Storage.GetClientTokens(updatedClient.ClientID)
if err != nil {
Expand Down
Loading