Skip to content

Commit 32eb609

Browse files
harshavardhanaminio-trusted
authored andcommitted
remove various unexpected features in console
- Unix listeners are removed - KeepAlive, IdleTimeout etc are removed - Authorization logic is simplified
1 parent c1e41e6 commit 32eb609

File tree

5 files changed

+81
-221
lines changed

5 files changed

+81
-221
lines changed

cmd/console/server.go

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/go-openapi/loads"
3030
"github.com/jessevdk/go-flags"
3131
"github.com/minio/cli"
32+
"github.com/minio/console/pkg/acl"
3233
"github.com/minio/console/pkg/certs"
3334
"github.com/minio/console/restapi"
3435
"github.com/minio/console/restapi/operations"
@@ -130,9 +131,8 @@ func StartServer(ctx *cli.Context) error {
130131

131132
server.Host = ctx.String("host")
132133
server.Port = ctx.Int("port")
133-
134-
restapi.Hostname = ctx.String("host")
135-
restapi.Port = strconv.Itoa(ctx.Int("port"))
134+
restapi.Hostname = server.Host
135+
restapi.Port = strconv.Itoa(server.Port)
136136

137137
// Set all certs and CAs directories path
138138
certs.GlobalCertsDir, _ = certs.NewConfigDirFromCtx(ctx, "certs-dir", certs.DefaultCertsDir.Get)
@@ -149,21 +149,21 @@ func StartServer(ctx *cli.Context) error {
149149
// TLS flags from swagger server, used to support VMware vsphere operator version.
150150
swaggerServerCertificate := ctx.String("tls-certificate")
151151
swaggerServerCertificateKey := ctx.String("tls-key")
152-
SwaggerServerCACertificate := ctx.String("tls-ca")
152+
swaggerServerCACertificate := ctx.String("tls-ca")
153153
// load tls cert and key from swagger server tls-certificate and tls-key flags
154154
if swaggerServerCertificate != "" && swaggerServerCertificateKey != "" {
155-
if errAddCert := certs.AddCertificate(context.Background(),
156-
restapi.GlobalTLSCertsManager, swaggerServerCertificate, swaggerServerCertificateKey); errAddCert != nil {
157-
log.Println(errAddCert)
155+
if err = certs.AddCertificate(context.Background(),
156+
restapi.GlobalTLSCertsManager, swaggerServerCertificate, swaggerServerCertificateKey); err != nil {
157+
log.Fatalln(err)
158158
}
159-
if x509Certs, errParseCert := certs.ParsePublicCertFile(swaggerServerCertificate); errParseCert == nil {
159+
if x509Certs, err := certs.ParsePublicCertFile(swaggerServerCertificate); err == nil {
160160
restapi.GlobalPublicCerts = append(restapi.GlobalPublicCerts, x509Certs...)
161161
}
162162
}
163163

164164
// load ca cert from swagger server tls-ca flag
165-
if SwaggerServerCACertificate != "" {
166-
caCert, caCertErr := ioutil.ReadFile(SwaggerServerCACertificate)
165+
if swaggerServerCACertificate != "" {
166+
caCert, caCertErr := ioutil.ReadFile(swaggerServerCACertificate)
167167
if caCertErr == nil {
168168
restapi.GlobalRootCAs.AppendCertsFromPEM(caCert)
169169
}
@@ -175,36 +175,37 @@ func StartServer(ctx *cli.Context) error {
175175
// plain HTTP connections to HTTPS server
176176
server.EnabledListeners = []string{"http", "https"}
177177
server.TLSPort = ctx.Int("tls-port")
178-
server.TLSHost = ctx.String("tls-host")
179178
// Need to store tls-port, tls-host un config variables so secure.middleware can read from there
180-
restapi.TLSPort = fmt.Sprintf("%v", ctx.Int("tls-port"))
179+
restapi.TLSPort = strconv.Itoa(server.TLSPort)
181180
restapi.Hostname = ctx.String("host")
182181
restapi.TLSRedirect = ctx.String("tls-redirect")
183182
}
184183

185184
server.ConfigureAPI()
186185

187-
// subnet license refresh process
188-
go func() {
189-
failedAttempts := 0
190-
for {
191-
if err := restapi.RefreshLicense(); err != nil {
192-
log.Println(err)
193-
failedAttempts++
194-
// end license refresh after 3 consecutive failed attempts
195-
if failedAttempts >= 3 {
196-
return
186+
if acl.GetOperatorMode() {
187+
// subnet license refresh process
188+
go func() {
189+
failedAttempts := 0
190+
for {
191+
if err := restapi.RefreshLicense(); err != nil {
192+
log.Println(err)
193+
failedAttempts++
194+
// end license refresh after 3 consecutive failed attempts
195+
if failedAttempts >= 3 {
196+
return
197+
}
198+
// wait 5 minutes and retry again
199+
time.Sleep(time.Minute * 5)
200+
continue
197201
}
198-
// wait 5 minutes and retry again
199-
time.Sleep(time.Minute * 5)
200-
continue
202+
// if license refreshed successfully reset the counter
203+
failedAttempts = 0
204+
// try to refresh license every 24 hrs
205+
time.Sleep(time.Hour * 24)
201206
}
202-
// if license refreshed successfully reset the counter
203-
failedAttempts = 0
204-
// try to refresh license every 24 hrs
205-
time.Sleep(time.Hour * 24)
206-
}
207-
}()
207+
}()
208+
}
208209

209210
if err := server.Serve(); err != nil {
210211
log.Fatalln(err)

pkg/auth/token.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import (
3232
"log"
3333
"net/http"
3434
"strings"
35+
"time"
3536

36-
"github.com/go-openapi/swag"
3737
"github.com/minio/console/models"
3838
"github.com/minio/console/pkg/auth/token"
3939
"github.com/minio/minio-go/v7/pkg/credentials"
@@ -43,8 +43,10 @@ import (
4343
"golang.org/x/crypto/pbkdf2"
4444
)
4545

46+
// Session token errors
4647
var (
47-
errNoAuthToken = errors.New("session token missing")
48+
ErrNoAuthToken = errors.New("session token missing")
49+
errTokenExpired = errors.New("session token has expired")
4850
errReadingToken = errors.New("session token internal data is malformed")
4951
errClaimsFormat = errors.New("encrypted session token claims not in the right format")
5052
errorGeneric = errors.New("an error has occurred")
@@ -82,7 +84,7 @@ type TokenClaims struct {
8284
// }
8385
func SessionTokenAuthenticate(token string) (*TokenClaims, error) {
8486
if token == "" {
85-
return nil, errNoAuthToken
87+
return nil, ErrNoAuthToken
8688
}
8789
// decrypt encrypted token
8890
claimTokens, err := decryptClaims(token)
@@ -289,25 +291,18 @@ func decrypt(ciphertext []byte, associatedData []byte) ([]byte, error) {
289291
// either defined on a cookie `token` or on Authorization header.
290292
//
291293
// Authorization Header needs to be like "Authorization Bearer <token>"
292-
func GetTokenFromRequest(r *http.Request) (*string, error) {
293-
// Get Auth token
294-
var reqToken string
295-
294+
func GetTokenFromRequest(r *http.Request) (string, error) {
296295
// Token might come either as a Cookie or as a Header
297296
// if not set in cookie, check if it is set on Header.
298297
tokenCookie, err := r.Cookie("token")
299298
if err != nil {
300-
headerToken := r.Header.Get("Authorization")
301-
// reqToken should come as "Bearer <token>"
302-
splitHeaderToken := strings.Split(headerToken, "Bearer")
303-
if len(splitHeaderToken) <= 1 {
304-
return nil, errNoAuthToken
305-
}
306-
reqToken = strings.TrimSpace(splitHeaderToken[1])
307-
} else {
308-
reqToken = strings.TrimSpace(tokenCookie.Value)
299+
return "", ErrNoAuthToken
300+
}
301+
currentTime := time.Now()
302+
if tokenCookie.Expires.After(currentTime) {
303+
return "", errTokenExpired
309304
}
310-
return swag.String(reqToken), nil
305+
return strings.TrimSpace(tokenCookie.Value), nil
311306
}
312307

313308
func GetClaimsFromTokenInRequest(req *http.Request) (*models.Principal, error) {
@@ -317,7 +312,7 @@ func GetClaimsFromTokenInRequest(req *http.Request) (*models.Principal, error) {
317312
}
318313
// Perform decryption of the session token, if Console is able to decrypt the session token that means a valid session
319314
// was used in the first place to get it
320-
claims, err := SessionTokenAuthenticate(*sessionID)
315+
claims, err := SessionTokenAuthenticate(sessionID)
321316
if err != nil {
322317
return nil, err
323318
}

restapi/admin_subscription.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -412,24 +412,19 @@ func RefreshLicense() error {
412412
if err != nil {
413413
return err
414414
}
415+
if refreshedLicenseKey == "" {
416+
return errors.New("license expired, please open a support ticket at https://subnet.min.io/")
417+
}
415418
// store new license in memory for console ui
416419
LicenseKey = refreshedLicenseKey
417-
// Update in memory license and update k8s secret
418-
if refreshedLicenseKey != "" {
419-
if acl.GetOperatorMode() {
420-
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
421-
defer cancel()
422-
clientSet, err := cluster.K8sClient(saK8SToken)
423-
if err != nil {
424-
return err
425-
}
426-
k8sClient := k8sClient{
427-
client: clientSet,
428-
}
429-
if err = saveSubscriptionLicense(ctx, &k8sClient, refreshedLicenseKey); err != nil {
430-
return err
431-
}
432-
}
420+
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
421+
defer cancel()
422+
clientSet, err := cluster.K8sClient(saK8SToken)
423+
if err != nil {
424+
return err
433425
}
434-
return nil
426+
k8sClient := k8sClient{
427+
client: clientSet,
428+
}
429+
return saveSubscriptionLicense(ctx, &k8sClient, refreshedLicenseKey)
435430
}

restapi/configure_console.go

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package restapi
2121
import (
2222
"bytes"
2323
"crypto/tls"
24-
"fmt"
2524
"io"
2625
"io/fs"
2726
"log"
@@ -169,13 +168,6 @@ func configureTLS(tlsConfig *tls.Config) {
169168
}
170169
}
171170

172-
// As soon as server is initialized but not run yet, this function will be called.
173-
// If you need to modify a config, store server instance to stop it individually later, this is the place.
174-
// This function can be called multiple times, depending on the number of serving schemes.
175-
// scheme value will be set accordingly: "http", "https" or "unix"
176-
func configureServer(s *http.Server, scheme, addr string) {
177-
}
178-
179171
// The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
180172
// The middleware executes after routing but before authentication, binding and validation
181173
func setupMiddlewares(handler http.Handler) http.Handler {
@@ -215,30 +207,22 @@ func setupGlobalMiddleware(handler http.Handler) http.Handler {
215207
IsDevelopment: !getProductionMode(),
216208
}
217209
secureMiddleware := secure.New(secureOptions)
218-
app := secureMiddleware.Handler(next)
219-
return app
210+
return secureMiddleware.Handler(next)
220211
}
221212

222213
func AuthenticationMiddleware(next http.Handler) http.Handler {
223214
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
224-
// prioritize authorization header and skip
225-
if r.Header.Get("Authorization") != "" {
226-
next.ServeHTTP(w, r)
215+
token, err := auth.GetTokenFromRequest(r)
216+
if err != nil && err != auth.ErrNoAuthToken {
217+
http.Error(w, err.Error(), http.StatusUnauthorized)
227218
return
228219
}
229-
tokenCookie, err := r.Cookie("token")
230-
if err != nil {
231-
next.ServeHTTP(w, r)
232-
return
233-
}
234-
currentTime := time.Now()
235-
if tokenCookie.Expires.After(currentTime) {
236-
next.ServeHTTP(w, r)
237-
return
238-
}
239-
token := tokenCookie.Value
220+
// All handlers handle appropriately to return errors
221+
// based on their swagger rules, we do not need to
222+
// additionally return error here, let the next ServeHTTPs
223+
// handle it appropriately.
240224
if token != "" {
241-
r.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
225+
r.Header.Add("Authorization", "Bearer "+token)
242226
}
243227
next.ServeHTTP(w, r)
244228
})
@@ -259,7 +243,6 @@ func FileServerMiddleware(next http.Handler) http.Handler {
259243
panic(err)
260244
}
261245
wrapHandlerSinglePageApplication(http.FileServer(http.FS(buildFs))).ServeHTTP(w, r)
262-
263246
}
264247
})
265248
}

0 commit comments

Comments
 (0)