github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_cidr.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package funcs
     7  
     8  import (
     9  	"fmt"
    10  	"net/netip"
    11  	"reflect"
    12  
    13  	"github.com/GuanceCloud/platypus/pkg/ast"
    14  	"github.com/GuanceCloud/platypus/pkg/engine/runtime"
    15  	"github.com/GuanceCloud/platypus/pkg/errchain"
    16  )
    17  
    18  func CIDRChecking(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    19  	if len(funcExpr.Param) != 2 {
    20  		return runtime.NewRunError(ctx, fmt.Sprintf(
    21  			"func %s expects 2 args", funcExpr.Name), funcExpr.NamePos)
    22  	}
    23  
    24  	param0 := funcExpr.Param[0]
    25  	if !isPlVarbOrFunc(param0) && param0.NodeType != ast.TypeStringLiteral {
    26  		return runtime.NewRunError(ctx, fmt.Sprintf(
    27  			"expect StringLiteral, Identifier or AttrExpr, got %s",
    28  			param0.NodeType), param0.StartPos())
    29  	}
    30  
    31  	param1 := funcExpr.Param[1]
    32  	if !isPlVarbOrFunc(param1) && param1.NodeType != ast.TypeStringLiteral {
    33  		return runtime.NewRunError(ctx, fmt.Sprintf(
    34  			"expect StringLiteral, Identifier or AttrExpr, got %s",
    35  			param1.NodeType), param1.StartPos())
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  func CIDR(ctx *runtime.Task, funcExpr *ast.CallExpr) *errchain.PlError {
    42  	if len(funcExpr.Param) != 2 {
    43  		err := fmt.Errorf("func %s expects 2 args", funcExpr.Name)
    44  		l.Debug(err)
    45  		ctx.Regs.ReturnAppend(false, ast.Bool)
    46  		return nil
    47  	}
    48  
    49  	ipAddr, err := getStr(ctx, funcExpr.Param[0])
    50  	if err != nil {
    51  		l.Debug(err)
    52  		ctx.Regs.ReturnAppend(false, ast.Bool)
    53  		return nil
    54  	}
    55  
    56  	prefix, err := getStr(ctx, funcExpr.Param[1])
    57  	if err != nil {
    58  		l.Debug(err)
    59  		ctx.Regs.ReturnAppend(false, ast.Bool)
    60  		return nil
    61  	}
    62  
    63  	ok, err := CIDRContains(ipAddr, prefix)
    64  	if err != nil {
    65  		l.Debug(err)
    66  	}
    67  
    68  	ctx.Regs.ReturnAppend(ok, ast.Bool)
    69  	return nil
    70  }
    71  
    72  func CIDRContains(ipAddr, prefix string) (bool, error) {
    73  	network, err := netip.ParsePrefix(prefix)
    74  	if err != nil {
    75  		return false, err
    76  	}
    77  
    78  	ip, err := netip.ParseAddr(ipAddr)
    79  	if err != nil {
    80  		return false, err
    81  	}
    82  
    83  	return network.Contains(ip), nil
    84  }
    85  
    86  func getStr(ctx *runtime.Task, node *ast.Node) (string, error) {
    87  	if node.NodeType == ast.TypeStringLiteral {
    88  		return node.StringLiteral().Val, nil
    89  	}
    90  
    91  	keyName, err := getKeyName(node)
    92  	if err != nil {
    93  		return "", err
    94  	}
    95  	v, err := ctx.GetKey(keyName)
    96  	if err != nil {
    97  		return "", err
    98  	}
    99  	if v.DType != ast.String {
   100  		return "", err
   101  	}
   102  
   103  	if addr, ok := v.Value.(string); ok {
   104  		return addr, nil
   105  	} else {
   106  		return "", fmt.Errorf("type: %s", reflect.TypeOf(v.Value).String())
   107  	}
   108  }