// Copyright (c) 2021. Huawei Technologies Co., Ltd. All rights reserved.

// Package common this file common validators
package common

import (
"crypto/tls"
"errors"
"net"
"regexp"
"time"

"huawei.com/npu-exporter/hwlog"
"huawei.com/npu-exporter/utils"

"huawei.com/mindxdl/base/common/constants"
)

const (
portLowerLimit = 1025
portUpperLimit = 40000
nameMaxLength = 63
nameMinLength = 3
serviceLogMaxSize = 200
nameFormat = `^[a-z0-9]+[a-z0-9-]*[a-z0-9]+$`
)

// ServerParam ServerParam
type ServerParam struct {
IP string
Port int
Version bool
Concurrency int
MaxConcurrency int
EnableHTTP bool
EncryptAlgorithm int
TLSSuites int
CipherSuites []uint16
AuthMode string
OverdueTime int
CheckCertPeriod time.Duration
DNSName string
DirName string
ReadTimeOut time.Duration
WriteTimeOut time.Duration
}

// GetDefaultServiceParam get default service param
func GetDefaultServiceParam() *ServerParam {
return &ServerParam{
MaxConcurrency: MaxConcurrency,
CheckCertPeriod: time.Hour,
AuthMode: TwoWay,
OverdueTime: utils.OverdueTime,
ReadTimeOut: HTTPServerTimeout,
WriteTimeOut: HTTPServerTimeout,
}
}

// WebParamValid doing param check
func WebParamValid(sp *ServerParam) error {
if sp.Port < portLowerLimit || sp.Port > portUpperLimit {
return errors.New("the port is invalid")
}
parsedIP := net.ParseIP(sp.IP)
if parsedIP == nil {
return errors.New("the listen ip is invalid")
}
sp.IP = parsedIP.String()
hwlog.RunLog.Infof("listen on: %s", sp.IP)

return nil
}

// ParseTLSParams ParseTLSParams
func ParseTLSParams(sp *ServerParam) {
if sp.EncryptAlgorithm != utils.Aes128gcm && sp.EncryptAlgorithm != utils.Aes256gcm {
hwlog.RunLog.Warn("reset invalid encryptAlgorithm = 9")
sp.EncryptAlgorithm = utils.Aes256gcm
}
if sp.TLSSuites != 0 && sp.TLSSuites != 1 {
hwlog.RunLog.Warn("reset invalid tlsSuites = 1")
sp.TLSSuites = 1
}
if sp.TLSSuites == 0 {
sp.CipherSuites = []uint16{tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256}
} else {
sp.CipherSuites = []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256}
}
}

// ValidName valid pod name or namespace
func ValidName(nameType, name string, b BaseCtx) bool {
if name == "" {
hwlog.RunLog.ErrorfWithCtx(b.Ctx, "%s is empty.", nameType)
return false
}
array := []rune(name)
if len(array) > nameMaxLength || len(array) < nameMinLength {
hwlog.RunLog.ErrorfWithCtx(b.Ctx, "%s length (%v) invalid.", nameType, len(array))
return false
}
var reg = regexp.MustCompile(nameFormat)
ok := reg.MatchString(name)
if !ok {
hwlog.RunLog.ErrorfWithCtx(b.Ctx, "%s is not meet requirement.", nameType)
return false
}
return true
}

func validLogLimitOffset(logOffset, logLimit uint64) error {
if logLimit > serviceLogMaxSize {
return errors.New("too large of limit")
}
if logOffset < 0 {
return errors.New("offset is less than 0")
}
return nil
}

// CheckString check string
func CheckString(passString string) error {
if matched, err := regexp.MatchString(constants.PassRegex, passString); err != nil || !matched {
return errors.New("password MatchString failed")
}

complexCheckRegexArr := []string{
constants.LowercaseCharactersRegex,
constants.UppercaseCharactersRegex,
constants.BaseNumberRegex,
constants.SpecialCharactersRegex,
}
complexCount := 0
for _, pattern := range complexCheckRegexArr {
if matched, err := regexp.MatchString(pattern, passString); matched && err == nil {
complexCount++
}
}
if complexCount < constants.MinComplexCount {
return errors.New("password complexCheck failed")
}
return nil
}

mindxdl--common--validators.go的更多相关文章

  1. RoR - More Active Record

    Active Record Scope: default_scope:   默认的检索方式 #Use unscoped to break out of the default class Hobby ...

  2. Yii2在Form中处理短信验证码的Validator,耦合度最低的短信验证码验证方式

    短信验证码在目前大多数web应用中都会有,本文介绍一个基于Yii2 Validator方式的验证码验证方式. 在其他文章中看到的方式大多比较难做到一次封装,多次重用. 使用此方式的好处自然不用多说,V ...

  3. ASP.NET - Validators

    ASP.NET validation controls validate the user input data to ensure that useless, unauthenticated, or ...

  4. Socket聊天程序——Common

    写在前面: 上一篇记录了Socket聊天程序的客户端设计,为了记录的完整性,这里还是将Socket聊天的最后一个模块--Common模块记录一下.Common的设计如下: 功能说明: Common模块 ...

  5. angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)

    common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...

  6. Common Bugs in C Programming

    There are some Common Bugs in C Programming. Most of the contents are directly from or modified from ...

  7. ANSI Common Lisp Practice - My Answers - Chatper - 3

    Ok, Go ahead. 1 (a) (b) (c) (d) 2 注:union 在 Common Lisp 中的作用就是求两个集合的并集.但是这有一个前提,即给的两个列表已经满足集合的属性了.具体 ...

  8. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  9. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  10. [LeetCode] Longest Common Prefix 最长共同前缀

    Write a function to find the longest common prefix string amongst an array of strings. 这道题让我们求一系列字符串 ...

随机推荐

  1. 使用IntelliJ IDEA新建一个spring boot项目

    好家伙, 使用IntelliJ IDEA新建一个spring boot项目 目的很简单,就是网页上出现一个"hello world" 别的暂时不管 首先关于工具IntelliJ I ...

  2. CSS基础第一篇:图片插入<img>,文本空格

    好家伙,这波是被迫回归基础 <img src="" alt=""> img代表"图像",它是图像在页面上显示.src代表&quo ...

  3. 为中小企业打造的数字化采购SaaS平台的特点与必要性

    ​激烈的市场竞争.复杂的国际环境.以及疫情的常态化将企业的供应链推向风口浪尖.供应链管理(SCM, Supply Chain Management).供应商关系管理(SRM,Supplier Rela ...

  4. 《网页设计基础——HTML注释与CSS注释》

    网页设计基础--HTML注释与CSS注释       一.HTML注释: 格式: <!-- 在此处书写注释 --> 例如: <html> <head> <ti ...

  5. 认识RocketMQ4.x架构设计

    消息模型 单体的消息模型 RocketMQ消息模型跟其他的消息队列一样 都是 producer - > topic->consumer producer 生产消息 也就是发送者 topic ...

  6. 使用 Skywalking Agent,这里使用sidecar 模式挂载 agent

    文章转载自:https://bbs.huaweicloud.com/blogs/315037 方法汇总 Java 中使用 agent ,提供了以下三种方式供你选择 使用官方提供的基础镜像 将 agen ...

  7. 单台主机MySQL多实例部署

    二进制安装mysql-5.7.26 [root@mysql ~]# cd /server/tools/ [root@mysql tools]# ll total 629756 -rw-r--r-- 1 ...

  8. 使用docker-compose部署WordPress项目

    创建空文件夹 假设新建一个名为 wordpress 的文件夹,然后进入这个文件夹. 创建 docker-compose.yml 文件 docker-compose.yml 文件将开启一个 wordpr ...

  9. 使用docker方式安装wordpress

    mkdir -p /home/my_wordpress cd my_wordpress/ vim docker-compose.yml version: '3.3' services: db: ima ...

  10. 小白入行安全圈内必须知道的top10

    OWASP Top10 前言 每年的Top10都在更新,但是一般不会有太大的改变,这里说明的是 2021年的Top10排行榜. A01:访问控制失效(Broken Access Control) 攻击 ...