2022-10-05 09:33:59 +00:00
package stackutils
import (
"fmt"
"regexp"
2024-06-12 23:06:17 +00:00
"strings"
2022-10-05 09:33:59 +00:00
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/filesystem"
)
func UserIsAdminOrEndpointAdmin ( user * portainer . User , endpointID portainer . EndpointID ) ( bool , error ) {
isAdmin := user . Role == portainer . AdministratorRole
return isAdmin , nil
}
// GetStackFilePaths returns a list of file paths based on stack project path
2023-04-03 21:00:17 +00:00
// If absolute is false, the path sanitization step will be skipped, which makes the returning
// paths vulnerable to path traversal attacks. Thus, the followed function using the returning
// paths are responsible to sanitize the raw paths
// If absolute is true, the raw paths will be sanitized
2022-10-05 09:33:59 +00:00
func GetStackFilePaths ( stack * portainer . Stack , absolute bool ) [ ] string {
if ! absolute {
return append ( [ ] string { stack . EntryPoint } , stack . AdditionalFiles ... )
}
var filePaths [ ] string
for _ , file := range append ( [ ] string { stack . EntryPoint } , stack . AdditionalFiles ... ) {
filePaths = append ( filePaths , filesystem . JoinPaths ( stack . ProjectPath , file ) )
}
2023-03-13 16:18:28 +00:00
2022-10-05 09:33:59 +00:00
return filePaths
}
// ResourceControlID returns the stack resource control id
func ResourceControlID ( endpointID portainer . EndpointID , name string ) string {
return fmt . Sprintf ( "%d_%s" , endpointID , name )
}
2024-06-12 23:06:17 +00:00
// convert string to valid kubernetes label by replacing invalid characters with periods and removing any periods at the beginning or end of the string
2022-10-05 09:33:59 +00:00
func SanitizeLabel ( value string ) string {
re := regexp . MustCompile ( ` [^A-Za-z0-9\.\-\_]+ ` )
2024-06-12 23:06:17 +00:00
onlyAllowedCharacterString := re . ReplaceAllString ( value , "." )
return strings . Trim ( onlyAllowedCharacterString , ".-_" )
2022-10-05 09:33:59 +00:00
}
2023-05-17 12:52:39 +00:00
// IsGitStack checks if the stack is a git stack or not
func IsGitStack ( stack * portainer . Stack ) bool {
return stack . GitConfig != nil && len ( stack . GitConfig . URL ) != 0
}
2023-08-28 23:49:00 +00:00
// IsRelativePathStack checks if the stack is a git stack or not
func IsRelativePathStack ( stack * portainer . Stack ) bool {
// Always return false in CE
// This function is only for code consistency with EE
return false
}