go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/authdb/internal/ipaddr/ipaddr.go (about)

     1  // Copyright 2019 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package ipaddr implements IP allowlist check.
    16  package ipaddr
    17  
    18  import (
    19  	"fmt"
    20  	"net"
    21  
    22  	"go.chromium.org/luci/auth/identity"
    23  
    24  	"go.chromium.org/luci/server/auth/service/protocol"
    25  )
    26  
    27  // Allowlist holds all named IP allowlist and the allowlist assignment map.
    28  type Allowlist struct {
    29  	assignments map[identity.Identity]string // identity => IP allowlist for it
    30  	allowlists  map[string][]net.IPNet       // IP allowlist => subnets
    31  }
    32  
    33  // NewAllowlist creates new populated IP allowlist set.
    34  func NewAllowlist(wl []*protocol.AuthIPWhitelist, as []*protocol.AuthIPWhitelistAssignment) (Allowlist, error) {
    35  	assignments := make(map[identity.Identity]string, len(as))
    36  	for _, a := range as {
    37  		assignments[identity.Identity(a.Identity)] = a.IpWhitelist
    38  	}
    39  
    40  	allowlists := make(map[string][]net.IPNet, len(wl))
    41  	for _, w := range wl {
    42  		if len(w.Subnets) == 0 {
    43  			continue
    44  		}
    45  		nets := make([]net.IPNet, len(w.Subnets))
    46  		for i, subnet := range w.Subnets {
    47  			_, ipnet, err := net.ParseCIDR(subnet)
    48  			if err != nil {
    49  				return Allowlist{}, fmt.Errorf("bad subnet %q in IP list %q - %s", subnet, w.Name, err)
    50  			}
    51  			nets[i] = *ipnet
    52  		}
    53  		allowlists[w.Name] = nets
    54  	}
    55  
    56  	return Allowlist{assignments, allowlists}, nil
    57  }
    58  
    59  // GetAllowlistForIdentity returns name of the IP allowlist to use to check
    60  // IP of requests from given `ident`.
    61  //
    62  // Returns an empty string if the identity is not IP-restricted.
    63  func (l Allowlist) GetAllowlistForIdentity(ident identity.Identity) string {
    64  	return l.assignments[ident]
    65  }
    66  
    67  // IsAllowedIP returns true if IP address belongs to given named IP allowlist.
    68  func (l Allowlist) IsAllowedIP(ip net.IP, allowlist string) bool {
    69  	for _, ipnet := range l.allowlists[allowlist] {
    70  		if ipnet.Contains(ip) {
    71  			return true
    72  		}
    73  	}
    74  	return false
    75  }