2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022-2023: Nordix Foundation
6 // Copyright (C) 2024: OpenInfra Foundation Europe
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 // ========================LICENSE_END===================================
32 "github.com/labstack/echo/v4"
33 copystructure "github.com/mitchellh/copystructure"
34 "k8s.io/utils/strings/slices"
35 "oransc.org/nonrtric/capifcore/internal/common29122"
36 securityapi "oransc.org/nonrtric/capifcore/internal/securityapi"
38 "oransc.org/nonrtric/capifcore/internal/invokermanagement"
39 "oransc.org/nonrtric/capifcore/internal/keycloak"
40 "oransc.org/nonrtric/capifcore/internal/providermanagement"
41 "oransc.org/nonrtric/capifcore/internal/publishservice"
44 type Security struct {
45 serviceRegister providermanagement.ServiceRegister
46 publishRegister publishservice.PublishRegister
47 invokerRegister invokermanagement.InvokerRegister
48 keycloak keycloak.AccessManagement
49 trustedInvokers map[string]securityapi.ServiceSecurity
53 func NewSecurity(serviceRegister providermanagement.ServiceRegister, publishRegister publishservice.PublishRegister, invokerRegister invokermanagement.InvokerRegister, km keycloak.AccessManagement) *Security {
55 serviceRegister: serviceRegister,
56 publishRegister: publishRegister,
57 invokerRegister: invokerRegister,
59 trustedInvokers: make(map[string]securityapi.ServiceSecurity),
63 func (s *Security) PostSecuritiesSecurityIdToken(ctx echo.Context, securityId string) error {
64 var accessTokenReq securityapi.AccessTokenReq
65 accessTokenReq.GetAccessTokenReq(ctx)
67 if valid, err := accessTokenReq.Validate(); !valid {
68 return ctx.JSON(http.StatusBadRequest, err)
71 if !s.invokerRegister.IsInvokerRegistered(accessTokenReq.ClientId) {
72 return sendAccessTokenError(ctx, http.StatusBadRequest, securityapi.AccessTokenErrErrorInvalidClient, "Invoker not registered")
75 if !s.invokerRegister.VerifyInvokerSecret(accessTokenReq.ClientId, *accessTokenReq.ClientSecret) {
76 return sendAccessTokenError(ctx, http.StatusBadRequest, securityapi.AccessTokenErrErrorUnauthorizedClient, "Invoker secret not valid")
79 if accessTokenReq.Scope != nil && *accessTokenReq.Scope != "" {
80 scope := strings.Split(*accessTokenReq.Scope, "#")
81 aefList := strings.Split(scope[1], ";")
82 for _, aef := range aefList {
83 apiList := strings.Split(aef, ":")
84 if !s.serviceRegister.IsFunctionRegistered(apiList[0]) {
85 return sendAccessTokenError(ctx, http.StatusBadRequest, securityapi.AccessTokenErrErrorInvalidScope, "AEF Function not registered")
87 for _, api := range strings.Split(apiList[1], ",") {
88 if !s.publishRegister.IsAPIPublished(apiList[0], api) {
89 return sendAccessTokenError(ctx, http.StatusBadRequest, securityapi.AccessTokenErrErrorInvalidScope, "API not published")
94 data := url.Values{"grant_type": {"client_credentials"}, "client_id": {accessTokenReq.ClientId}, "client_secret": {*accessTokenReq.ClientSecret}}
95 jwtToken, err := s.keycloak.GetToken("invokerrealm", data)
97 return sendAccessTokenError(ctx, http.StatusBadRequest, securityapi.AccessTokenErrErrorUnauthorizedClient, err.Error())
100 accessTokenResp := securityapi.AccessTokenRsp{
101 AccessToken: jwtToken.AccessToken,
102 ExpiresIn: common29122.DurationSec(jwtToken.ExpiresIn),
103 Scope: accessTokenReq.Scope,
107 err = ctx.JSON(http.StatusCreated, accessTokenResp)
109 // Something really bad happened, tell Echo that our handler failed
116 func (s *Security) DeleteTrustedInvokersApiInvokerId(ctx echo.Context, apiInvokerId string) error {
117 if _, ok := s.trustedInvokers[apiInvokerId]; ok {
118 s.deleteTrustedInvoker(apiInvokerId)
121 return ctx.NoContent(http.StatusNoContent)
124 func (s *Security) deleteTrustedInvoker(apiInvokerId string) {
126 defer s.lock.Unlock()
127 delete(s.trustedInvokers, apiInvokerId)
130 func (s *Security) GetTrustedInvokersApiInvokerId(ctx echo.Context, apiInvokerId string, params securityapi.GetTrustedInvokersApiInvokerIdParams) error {
132 if trustedInvoker, ok := s.trustedInvokers[apiInvokerId]; ok {
133 updatedInvoker := s.checkParams(trustedInvoker, params)
134 if updatedInvoker != nil {
135 err := ctx.JSON(http.StatusOK, updatedInvoker)
141 return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf("invoker %s not registered as trusted invoker", apiInvokerId))
147 func (s *Security) checkParams(trustedInvoker securityapi.ServiceSecurity, params securityapi.GetTrustedInvokersApiInvokerIdParams) *securityapi.ServiceSecurity {
150 var sendAuthenticationInfo = (params.AuthenticationInfo != nil) && *params.AuthenticationInfo
151 var sendAuthorizationInfo = (params.AuthorizationInfo != nil) && *params.AuthorizationInfo
153 if sendAuthenticationInfo && sendAuthorizationInfo {
154 return &trustedInvoker
157 data, _ := copystructure.Copy(trustedInvoker)
158 updatedInvoker, ok := data.(securityapi.ServiceSecurity)
163 if !sendAuthenticationInfo {
164 for i := range updatedInvoker.SecurityInfo {
165 updatedInvoker.SecurityInfo[i].AuthenticationInfo = &emptyString
168 if !sendAuthorizationInfo {
169 for i := range updatedInvoker.SecurityInfo {
170 updatedInvoker.SecurityInfo[i].AuthorizationInfo = &emptyString
173 return &updatedInvoker
176 func (s *Security) PutTrustedInvokersApiInvokerId(ctx echo.Context, apiInvokerId string) error {
177 errMsg := "Unable to update security context due to %s."
179 if !s.invokerRegister.IsInvokerRegistered(apiInvokerId) {
180 return sendCoreError(ctx, http.StatusBadRequest, "Unable to update security context due to Invoker not registered")
182 serviceSecurity, err := getServiceSecurityFromRequest(ctx)
184 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
187 if err := serviceSecurity.Validate(); err != nil {
188 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
191 err = s.prepareNewSecurityContext(&serviceSecurity, apiInvokerId)
193 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
196 uri := ctx.Request().Host + ctx.Request().URL.String()
197 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, apiInvokerId))
199 err = ctx.JSON(http.StatusCreated, s.trustedInvokers[apiInvokerId])
201 // Something really bad happened, tell Echo that our handler failed
208 func getServiceSecurityFromRequest(ctx echo.Context) (securityapi.ServiceSecurity, error) {
209 var serviceSecurity securityapi.ServiceSecurity
210 err := ctx.Bind(&serviceSecurity)
212 return securityapi.ServiceSecurity{}, fmt.Errorf("invalid format for service security")
214 return serviceSecurity, nil
217 func (s *Security) prepareNewSecurityContext(newContext *securityapi.ServiceSecurity, apiInvokerId string) error {
219 defer s.lock.Unlock()
221 err := newContext.PrepareNewSecurityContext(s.publishRegister.GetAllPublishedServices())
226 s.trustedInvokers[apiInvokerId] = *newContext
230 func (s *Security) PostTrustedInvokersApiInvokerIdDelete(ctx echo.Context, apiInvokerId string) error {
231 var notification securityapi.SecurityNotification
233 errMsg := "Unable to revoke invoker due to %s"
235 if err := ctx.Bind(¬ification); err != nil {
236 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for security notification"))
239 if err := notification.Validate(); err != nil {
240 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
243 if ss, ok := s.trustedInvokers[apiInvokerId]; ok {
244 securityInfoCopy := s.revokeTrustedInvoker(&ss, notification)
246 if len(securityInfoCopy) == 0 {
247 s.deleteTrustedInvoker(apiInvokerId)
249 ss.SecurityInfo = securityInfoCopy
250 s.updateTrustedInvoker(ss, apiInvokerId)
254 return sendCoreError(ctx, http.StatusNotFound, "the invoker is not register as a trusted invoker")
257 return ctx.NoContent(http.StatusNoContent)
261 func (s *Security) revokeTrustedInvoker(ss *securityapi.ServiceSecurity, notification securityapi.SecurityNotification) []securityapi.SecurityInformation {
263 data, _ := copystructure.Copy(ss.SecurityInfo)
264 securityInfoCopy, _ := data.([]securityapi.SecurityInformation)
266 for i, context := range ss.SecurityInfo {
267 if notification.AefId == context.AefId || slices.Contains(notification.ApiIds, *context.ApiId) {
268 securityInfoCopy = append(securityInfoCopy[:i], securityInfoCopy[i+1:]...)
272 return securityInfoCopy
276 func (s *Security) PostTrustedInvokersApiInvokerIdUpdate(ctx echo.Context, apiInvokerId string) error {
277 var serviceSecurity securityapi.ServiceSecurity
279 errMsg := "Unable to update service security context due to %s"
281 if err := ctx.Bind(&serviceSecurity); err != nil {
282 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for service security context"))
285 if err := serviceSecurity.Validate(); err != nil {
286 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
289 if _, ok := s.trustedInvokers[apiInvokerId]; ok {
290 s.updateTrustedInvoker(serviceSecurity, apiInvokerId)
292 return sendCoreError(ctx, http.StatusNotFound, "the invoker is not register as a trusted invoker")
295 uri := ctx.Request().Host + ctx.Request().URL.String()
296 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, apiInvokerId))
298 err := ctx.JSON(http.StatusOK, s.trustedInvokers[apiInvokerId])
300 // Something really bad happened, tell Echo that our handler failed
307 func (s *Security) updateTrustedInvoker(serviceSecurity securityapi.ServiceSecurity, invokerId string) {
309 defer s.lock.Unlock()
310 s.trustedInvokers[invokerId] = serviceSecurity
313 func sendAccessTokenError(ctx echo.Context, code int, err securityapi.AccessTokenErrError, message string) error {
314 accessTokenErr := securityapi.AccessTokenErr{
316 ErrorDescription: &message,
318 return ctx.JSON(code, accessTokenErr)
321 // This function wraps sending of an error in the Error format, and
322 // handling the failure to marshal that.
323 func sendCoreError(ctx echo.Context, code int, message string) error {
324 pd := common29122.ProblemDetails{
328 err := ctx.JSON(code, pd)