2022-07-05 21:18:04 -04:00
|
|
|
// Copyright (c) 2015-2022 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-10-19 14:00:10 -04:00
|
|
|
"errors"
|
2022-07-05 21:18:04 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/minio/madmin-go"
|
|
|
|
"github.com/minio/minio/internal/config"
|
|
|
|
"github.com/minio/minio/internal/config/identity/openid"
|
|
|
|
"github.com/minio/minio/internal/logger"
|
|
|
|
iampolicy "github.com/minio/pkg/iam/policy"
|
2022-10-19 14:00:10 -04:00
|
|
|
"github.com/minio/pkg/ldap"
|
2022-07-05 21:18:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetIdentityProviderCfg:
|
|
|
|
//
|
|
|
|
// PUT <admin-prefix>/id-cfg?type=openid&name=dex1
|
|
|
|
func (a adminAPIHandlers) SetIdentityProviderCfg(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "SetIdentityCfg")
|
|
|
|
|
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
|
|
|
|
|
|
|
objectAPI, cred := validateAdminReq(ctx, w, r, iampolicy.ConfigUpdateAdminAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.ContentLength > maxEConfigJSONSize || r.ContentLength == -1 {
|
|
|
|
// More than maxConfigSize bytes were available
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigTooLarge), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
password := cred.SecretKey
|
|
|
|
reqBytes, err := madmin.DecryptData(password, io.LimitReader(r.Body, r.ContentLength))
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err, logger.Application)
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigBadJSON), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
idpCfgType := mux.Vars(r)["type"]
|
|
|
|
if !madmin.ValidIDPConfigTypes.Contains(idpCfgType) {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigInvalidIDPType), r.URL)
|
2022-07-05 21:18:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfgDataBuilder strings.Builder
|
2022-10-19 14:00:10 -04:00
|
|
|
switch idpCfgType {
|
|
|
|
case madmin.OpenidIDPCfg:
|
2022-07-05 21:18:04 -04:00
|
|
|
fmt.Fprintf(&cfgDataBuilder, "identity_openid")
|
2022-10-19 14:00:10 -04:00
|
|
|
case madmin.LDAPIDPCfg:
|
|
|
|
fmt.Fprintf(&cfgDataBuilder, "identity_ldap")
|
2022-07-05 21:18:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure body content type is opaque.
|
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
if contentType != "application/octet-stream" {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrBadRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subsystem configuration name could be empty.
|
|
|
|
cfgName := mux.Vars(r)["name"]
|
|
|
|
if cfgName != "" {
|
2022-10-19 14:00:10 -04:00
|
|
|
if idpCfgType == madmin.LDAPIDPCfg {
|
|
|
|
// LDAP does not support multiple configurations. So this must be
|
|
|
|
// empty.
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrBadRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-05 21:18:04 -04:00
|
|
|
fmt.Fprintf(&cfgDataBuilder, "%s%s", config.SubSystemSeparator, cfgName)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(&cfgDataBuilder, "%s%s", config.KvSpaceSeparator, string(reqBytes))
|
|
|
|
|
|
|
|
cfgData := cfgDataBuilder.String()
|
|
|
|
subSys, _, _, err := config.GetSubSys(cfgData)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := readServerConfig(ctx, objectAPI)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamic, err := cfg.ReadConfig(strings.NewReader(cfgData))
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDP config is not dynamic. Sanity check.
|
|
|
|
if dynamic {
|
|
|
|
writeCustomErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInternalError), err.Error(), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = validateConfig(cfg, subSys); err != nil {
|
2022-10-19 14:00:10 -04:00
|
|
|
|
|
|
|
var validationErr ldap.Validation
|
|
|
|
if errors.As(err, &validationErr) {
|
|
|
|
// If we got an LDAP validation error, we need to send appropriate
|
|
|
|
// error message back to client (likely mc).
|
|
|
|
writeCustomErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigLDAPValidation),
|
|
|
|
validationErr.FormatError(), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-05 21:18:04 -04:00
|
|
|
writeCustomErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigBadJSON), err.Error(), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the actual server config on disk.
|
|
|
|
if err = saveServerConfig(ctx, objectAPI, cfg); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the config input KV to history.
|
|
|
|
if err = saveServerConfigHistory(ctx, objectAPI, []byte(cfgData)); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseHeadersOnly(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIdentityProviderCfg:
|
|
|
|
//
|
|
|
|
// GET <admin-prefix>/id-cfg?type=openid&name=dex_test
|
|
|
|
//
|
|
|
|
// GetIdentityProviderCfg returns a list of configured IDPs on the server if
|
|
|
|
// name is empty. If name is non-empty, returns the configuration details for
|
|
|
|
// the IDP of the given type and configuration name. The configuration name for
|
|
|
|
// the default ("un-named") configuration target is `_`.
|
|
|
|
func (a adminAPIHandlers) GetIdentityProviderCfg(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "GetIdentityProviderCfg")
|
|
|
|
|
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
|
|
|
|
|
|
|
objectAPI, cred := validateAdminReq(ctx, w, r, iampolicy.ConfigUpdateAdminAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
idpCfgType := mux.Vars(r)["type"]
|
2022-07-05 21:18:04 -04:00
|
|
|
cfgName := r.Form.Get("name")
|
|
|
|
password := cred.SecretKey
|
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
if !madmin.ValidIDPConfigTypes.Contains(idpCfgType) {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigInvalidIDPType), r.URL)
|
2022-07-05 21:18:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no cfgName is provided, we list.
|
|
|
|
if cfgName == "" {
|
2022-10-19 14:00:10 -04:00
|
|
|
a.listIdentityProviders(ctx, w, r, idpCfgType, password)
|
2022-07-05 21:18:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := globalServerConfig.Clone()
|
2022-10-19 14:00:10 -04:00
|
|
|
var cfgInfos []madmin.IDPCfgInfo
|
|
|
|
var err error
|
|
|
|
switch idpCfgType {
|
|
|
|
case madmin.OpenidIDPCfg:
|
|
|
|
cfgInfos, err = globalOpenIDConfig.GetConfigInfo(cfg, cfgName)
|
|
|
|
case madmin.LDAPIDPCfg:
|
|
|
|
cfgInfos, err = globalLDAPConfig.GetConfigInfo(cfg, cfgName)
|
|
|
|
}
|
2022-07-05 21:18:04 -04:00
|
|
|
if err != nil {
|
2022-10-19 14:00:10 -04:00
|
|
|
if errors.Is(err, openid.ErrProviderConfigNotFound) {
|
2022-07-05 21:18:04 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminNoSuchConfigTarget), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := madmin.IDPConfig{
|
2022-10-19 14:00:10 -04:00
|
|
|
Type: idpCfgType,
|
2022-07-05 21:18:04 -04:00
|
|
|
Name: cfgName,
|
|
|
|
Info: cfgInfos,
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
econfigData, err := madmin.EncryptData(password, data)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, econfigData)
|
|
|
|
}
|
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
func (a adminAPIHandlers) listIdentityProviders(ctx context.Context, w http.ResponseWriter, r *http.Request, idpCfgType, password string) {
|
|
|
|
var cfgList []madmin.IDPListItem
|
|
|
|
var err error
|
|
|
|
switch idpCfgType {
|
|
|
|
case madmin.OpenidIDPCfg:
|
|
|
|
cfg := globalServerConfig.Clone()
|
|
|
|
cfgList, err = globalOpenIDConfig.GetConfigList(cfg)
|
|
|
|
case madmin.LDAPIDPCfg:
|
|
|
|
cfg := globalServerConfig.Clone()
|
|
|
|
cfgList, err = globalLDAPConfig.GetConfigList(cfg)
|
|
|
|
|
2022-07-05 21:18:04 -04:00
|
|
|
default:
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(cfgList)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
econfigData, err := madmin.EncryptData(password, data)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, econfigData)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteIdentityProviderCfg:
|
|
|
|
//
|
|
|
|
// DELETE <admin-prefix>/id-cfg?type=openid&name=dex_test
|
|
|
|
func (a adminAPIHandlers) DeleteIdentityProviderCfg(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "DeleteIdentityProviderCfg")
|
|
|
|
|
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
|
|
|
|
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, iampolicy.ConfigUpdateAdminAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
idpCfgType := mux.Vars(r)["type"]
|
2022-07-05 21:18:04 -04:00
|
|
|
cfgName := mux.Vars(r)["name"]
|
2022-10-19 14:00:10 -04:00
|
|
|
if !madmin.ValidIDPConfigTypes.Contains(idpCfgType) {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigInvalidIDPType), r.URL)
|
2022-07-05 21:18:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
cfgCopy := globalServerConfig.Clone()
|
|
|
|
var subSys string
|
|
|
|
switch idpCfgType {
|
|
|
|
case madmin.OpenidIDPCfg:
|
|
|
|
subSys = config.IdentityOpenIDSubSys
|
|
|
|
cfgInfos, err := globalOpenIDConfig.GetConfigInfo(cfgCopy, cfgName)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, openid.ErrProviderConfigNotFound) {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminNoSuchConfigTarget), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
2022-07-05 21:18:04 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
hasEnv := false
|
|
|
|
for _, ci := range cfgInfos {
|
|
|
|
if ci.IsCfg && ci.IsEnv {
|
|
|
|
hasEnv = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 21:18:04 -04:00
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
if hasEnv {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigEnvOverridden), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case madmin.LDAPIDPCfg:
|
|
|
|
subSys = config.IdentityLDAPSubSys
|
|
|
|
cfgInfos, err := globalLDAPConfig.GetConfigInfo(cfgCopy, cfgName)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, openid.ErrProviderConfigNotFound) {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminNoSuchConfigTarget), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
2022-07-05 21:18:04 -04:00
|
|
|
}
|
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
hasEnv := false
|
|
|
|
for _, ci := range cfgInfos {
|
|
|
|
if ci.IsCfg && ci.IsEnv {
|
|
|
|
hasEnv = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-07-05 21:18:04 -04:00
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
if hasEnv {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigEnvOverridden), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2022-07-05 21:18:04 -04:00
|
|
|
default:
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
cfg, err := readServerConfig(ctx, objectAPI)
|
2022-07-05 21:18:04 -04:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-19 14:00:10 -04:00
|
|
|
cfgKey := fmt.Sprintf("%s:%s", subSys, cfgName)
|
|
|
|
if cfgName == madmin.Default {
|
|
|
|
cfgKey = subSys
|
|
|
|
}
|
|
|
|
if err = cfg.DelKVS(cfgKey); err != nil {
|
2022-07-05 21:18:04 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = validateConfig(cfg, subSys); err != nil {
|
|
|
|
writeCustomErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminConfigBadJSON), err.Error(), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = saveServerConfig(ctx, objectAPI, cfg); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamic := config.SubSystemsDynamic.Contains(subSys)
|
|
|
|
if dynamic {
|
|
|
|
applyDynamic(ctx, objectAPI, cfg, subSys, r, w)
|
|
|
|
}
|
|
|
|
}
|