- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2.9k
feat: IP restriction #6221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
        
      
            qxo
  wants to merge
  1
  commit into
  mudler:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
qxo:feat-webui-ip-limit
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    feat: IP restriction #6221
Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package utils | ||
|  | ||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "net/netip" | ||
| "strings" | ||
| "sync" | ||
| ) | ||
|  | ||
| type IPAllowList struct { | ||
| allowList string | ||
| cidrs []*net.IPNet | ||
| ips []net.IP | ||
| mu sync.RWMutex | ||
| enabled bool | ||
| } | ||
|  | ||
| func NewIPAllowList(allowList string) (*IPAllowList, error) { | ||
|  | ||
| w := &IPAllowList{} | ||
| err := w.Update(allowList) | ||
| return w, err | ||
| } | ||
|  | ||
| func (w *IPAllowList) GetAllowList() string { | ||
| return w.allowList | ||
| } | ||
|  | ||
| func (w *IPAllowList) Update(allowListStr string) error { | ||
| var cidrs []*net.IPNet | ||
| var ips []net.IP | ||
|  | ||
| allowList := make([]string, 0) | ||
| if allowListStr != "" { | ||
| allowList = strings.Split(allowListStr, ",") | ||
| } | ||
|  | ||
| for _, item := range allowList { | ||
| _, cidrNet, err := net.ParseCIDR(item) | ||
| if err == nil { | ||
| cidrs = append(cidrs, cidrNet) | ||
| } else { | ||
| ip := net.ParseIP(item) | ||
| if ip != nil { | ||
| ips = append(ips, ip) | ||
| } else { | ||
| return fmt.Errorf("invalid allowList item: %s", item) | ||
| } | ||
| } | ||
| } | ||
|  | ||
| w.mu.Lock() | ||
| defer w.mu.Unlock() | ||
| w.allowList = allowListStr | ||
| w.cidrs = cidrs | ||
| w.ips = ips | ||
| w.enabled = len(cidrs) > 0 || len(ips) > 0 | ||
| return nil | ||
| } | ||
|  | ||
| func (w *IPAllowList) IsAllowed(ip interface{}) bool { | ||
| if !w.enabled { | ||
| return true | ||
| } | ||
|  | ||
| var parsedIP net.IP | ||
| switch v := ip.(type) { | ||
| case string: | ||
| parsedIP = net.ParseIP(v) | ||
| case net.IP: | ||
| parsedIP = v | ||
| case netip.Addr: | ||
| parsedIP = net.IP(v.AsSlice()) | ||
| default: | ||
| if str, ok := v.(string); ok { | ||
| parsedIP = net.ParseIP(str) | ||
| } | ||
| } | ||
|  | ||
| if parsedIP == nil { | ||
| return false | ||
| } | ||
|  | ||
| w.mu.RLock() | ||
| defer w.mu.RUnlock() | ||
|  | ||
| for _, cidr := range w.cidrs { | ||
| if cidr.Contains(parsedIP) { | ||
| return true | ||
| } | ||
| } | ||
|  | ||
| for _, allowedIP := range w.ips { | ||
| if parsedIP.Equal(allowedIP) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package utils | ||
|  | ||
| import ( | ||
| "fmt" | ||
| "testing" | ||
| ) | ||
|  | ||
| func TestIPAllowList(t *testing.T) { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better be consistent with all the other tests in this repository and use ginkgo | ||
| // Test empty AllowList (no restrictions) | ||
| w, err := NewIPAllowList("") | ||
|  | ||
| if err != nil { | ||
| t.Fatalf("Expected no error for empty AllowList, got: %v", err) | ||
| } | ||
| if !w.IsAllowed("192.168.1.100") { | ||
| t.Error("Empty AllowList should allow all IPs") | ||
| } | ||
|  | ||
| // Test valid AllowList | ||
| AllowList := "192.168.1.0/24,10.0.0.1,127.0.0.1" | ||
| w, err = NewIPAllowList(AllowList) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create IP AllowList: %v", err) | ||
| } | ||
|  | ||
| tests := []struct { | ||
| ip string | ||
| expected bool | ||
| }{ | ||
| {"192.168.1.100", true}, | ||
| {"10.0.0.1", true}, | ||
| {"127.0.0.1", true}, | ||
| {"10.0.0.2", false}, | ||
| {"172.16.0.1", false}, | ||
| } | ||
|  | ||
| for _, tc := range tests { | ||
| t.Run(fmt.Sprintf("IP: %s", tc.ip), func(t *testing.T) { | ||
| if got := w.IsAllowed(tc.ip); got != tc.expected { | ||
| t.Errorf("isAllowedIP(%q) = %v, want %v", tc.ip, got, tc.expected) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks better placed as
ipallowlist.gofile insidecore/httprather than having it's ownutilspackage.